# \[solved\] Cannot handle exception MIME::Multipart::Error

**URL:** <https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868>\
**Category:** Help & Support\
**Created:** [July 23, 2023, 3:29pm UTC](https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868 "2023-07-23T15:29:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulo.f.rodriguez](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/paulo.f.rodriguez/32/2089_2.png) [@paulo.f.rodriguez](https://forum.crystal-lang.org/u/paulo.f.rodriguez)\
**Post date:** [July 23, 2023, 3:29pm UTC](https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868/1 "2023-07-23T15:29:10Z")

</div>

Hi, probably is a stupid error ,but I cannot handle this exception on the code below.  
I am breaking the upload in the middle and my idea is to clean up the temp file that is incomplete.

```auto
require "kemal"
require "json"

require "./services/request_id_gen"

Kemal.config.public_folder = "/tmp"

post "/alt" do |env|
  file_data = env.params.files["image1"]

  begin

    if file_data && file_data.tempfile.size > 0

      upload_id = "#{Time.utc.to_unix.to_s}-#{get_req_id()}"

      original_filename = file_data.filename
      file_path = ::File.join([Kemal.config.public_folder, "uploads/", original_filename])

      File.rename(file_data.tempfile.path, file_path)

      "upload complete id: #{upload_id}"
    else
      "Error"
    end
  rescue ex : MIME::Multipart::Error

    if file_data
      file_data.tempfile.delete
    end
    
  end
end

Kemal.run

```

I have this exception several seconds after I break the upload:

```auto
development] Kemal is ready to lead at http://0.0.0.0:3000
Exception: Failed to parse multipart message: EOF reading delimiter (MIME::Multipart::Error)
  from /usr/share/crystal/src/mime/multipart/parser.cr:126:7 in 'fail'
  from /usr/share/crystal/src/mime/multipart/parser.cr:111:7 in 'close_delimiter?'
  from /usr/share/crystal/src/mime/multipart/parser.cr:73:39 in 'parse_files'
  from lib/kemal/src/kemal/param_parser.cr:32:5 in 'files'
  from src/alt.cr:12:15 in '->'
  from lib/kemal/src/kemal/route.cr:12:26 in '->'
  from lib/kemal/src/kemal/route_handler.cr:52:39 in 'process_request'
  from lib/kemal/src/kemal/route_handler.cr:17:7 in 'call'
  from /usr/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/websocket_handler.cr:13:14 in 'call'
  from /usr/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/static_file_handler.cr:11:11 in 'call'
  from /usr/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/exception_handler.cr:8:7 in 'call'
  from /usr/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/head_request_handler.cr:57:7 in 'call'
  from /usr/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from /usr/share/crystal/src/time.cr:357:5 in 'call'
  from /usr/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/init_handler.cr:12:7 in 'call'
  from /usr/share/crystal/src/http/server/request_processor.cr:51:11 in 'process'
  from /usr/share/crystal/src/http/server.cr:521:5 in 'handle_client'
  from /usr/share/crystal/src/http/server.cr:451:5 in '->'
  from /usr/share/crystal/src/fiber.cr:146:11 in 'run'
  from /usr/share/crystal/src/fiber.cr:98:34 in '->'
  from ???

```

---

<div class="post-metadata">

**Author:** ![paulo.f.rodriguez](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/paulo.f.rodriguez/32/2089_2.png) [@paulo.f.rodriguez](https://forum.crystal-lang.org/u/paulo.f.rodriguez)\
**Post date:** [July 23, 2023, 7:07pm UTC](https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868/2 "2023-07-23T19:07:08Z")

</div>

file\_data = env.params.files[“image1”] should be after the begin …

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [July 25, 2023, 6:39am UTC](https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868/3 "2023-07-25T06:39:58Z")

</div>

> [@paulo.f.rodriguez](#):
>
> ` file_data = env.params.files["image1"]`

How you send the request to make `env.params.files` not always a empty `{}`?

what is your’s kemal/crystal version?

---

<div class="post-metadata">

**Author:** ![paulo.f.rodriguez](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/paulo.f.rodriguez/32/2089_2.png) [@paulo.f.rodriguez](https://forum.crystal-lang.org/u/paulo.f.rodriguez)\
**Post date:** [July 25, 2023, 12:01pm UTC](https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868/4 "2023-07-25T12:01:48Z")

</div>

latest for both. I simply can use like that. My problem is , if I break the upload, it gets nil and I can`t handle the exception or handle to clean up the broken temporary file.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [July 25, 2023, 3:11pm UTC](https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868/5 "2023-07-25T15:11:26Z")

</div>

Could you please give a `curl` command for show me how to make a request to your server and make `env.params.files` not empty?

---

<div class="post-metadata">

**Author:** ![paulo.f.rodriguez](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/paulo.f.rodriguez/32/2089_2.png) [@paulo.f.rodriguez](https://forum.crystal-lang.org/u/paulo.f.rodriguez)\
**Post date:** [July 25, 2023, 8:01pm UTC](https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868/6 "2023-07-25T20:01:53Z")

</div>

curl -X POST -F “image1=@file8” [http://localhost:3000/alt](http://localhost:3000/alt) -o test

and I generate the file with fallocate -l 8G file8

the parameter -l is lowercase L

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [July 26, 2023, 3:09am UTC](https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868/7 "2023-07-26T03:09:52Z")

</div>

I don’t know why, your sample code just not work for me, please check following error backtrace.

```auto
curl -X POST -F “image1=crystal.png” http://localhost:3000/alt -o test

```

```auto
[development] Kemal is ready to lead at http://0.0.0.0:3000
Exception: Missing hash key: "image1" (KeyError)
  from /home/zw963/Crystal/share/crystal/src/hash.cr:1077:11 in '[]'
  from src/file_upload_kemal.cr:7:15 in '->'
  from lib/kemal/src/kemal/route.cr:12:26 in '->'
  from lib/kemal/src/kemal/route_handler.cr:52:39 in 'process_request'
  from lib/kemal/src/kemal/route_handler.cr:17:7 in 'call'
  from /home/zw963/Crystal/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/websocket_handler.cr:13:14 in 'call'
  from /home/zw963/Crystal/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/static_file_handler.cr:11:11 in 'call'
  from /home/zw963/Crystal/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/exception_handler.cr:8:7 in 'call'
  from /home/zw963/Crystal/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/head_request_handler.cr:57:7 in 'call'
  from /home/zw963/Crystal/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from /home/zw963/Crystal/share/crystal/src/time.cr:357:5 in 'call'
  from /home/zw963/Crystal/share/crystal/src/http/server/handler.cr:30:7 in 'call_next'
  from lib/kemal/src/kemal/init_handler.cr:12:7 in 'call'
  from /home/zw963/Crystal/share/crystal/src/http/server/request_processor.cr:51:11 in 'process'
  from /home/zw963/Crystal/share/crystal/src/http/server.cr:521:5 in 'handle_client'
  from /home/zw963/Crystal/share/crystal/src/http/server.cr:451:5 in '->'
  from /home/zw963/Crystal/share/crystal/src/fiber.cr:146:11 in 'run'
  from /home/zw963/Crystal/share/crystal/src/fiber.cr:98:34 in '->'
  from ???

2023-07-26 03:07:19 UTC 500 POST /alt 165.1ms

```

---

<div class="post-metadata">

**Author:** ![paulo.f.rodriguez](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/paulo.f.rodriguez/32/2089_2.png) [@paulo.f.rodriguez](https://forum.crystal-lang.org/u/paulo.f.rodriguez)\
**Post date:** [July 27, 2023, 11:59pm UTC](https://forum.crystal-lang.org/t/solved-cannot-handle-exception-mime-error/5868/8 "2023-07-27T23:59:21Z")

</div>

try it over and worked like a charm…

check if your shards are like mine…

```auto
name: req_tool
version: 0.1.0

authors:
  (ommited)

dependencies:
  kemal:
    github: kemalcr/kemal
    version: ~> 1.4.0
  amqp:
    github: datanoise/amqp.cr
  io-delimited-optimized:
    git: https://gist.github.com/jgaskins/91186b3451988f714b3bebe45b63e29d

targets:
  req_tool:
    main: src/req_tool.cr

crystal: 1.9.2

license: MIT

```
