duct-framework / module.web

Duct module for running web applications
21 stars 20 forks source link

Uncaught exception when a request body is not valid e.g. JSON #8

Open LukasRychtecky opened 6 years ago

LukasRychtecky commented 6 years ago

Hi,

I found that a usage of muuntaja.middleware/wrap-format is used in duct.middleware.web/format component without wrapping it into muuntaja.middleware/wrap-exception thus when a request body contains invalid JSON an exception is raised and couldn't be caught.

Quick and dirty fix could be like this:

(defmethod ig/init-key ::format [_ options]
  (fn [a]
    (mm/wrap-exception
      (mm/wrap-format a (deep-merge mc/default-options options))
      (fn [ex fmt request]
         {:status 400
          :headers {"Content-Type" "text/plain"}
          :body (str "Malformed " format " request.")}))))

But I think the best would be to allow a developer to define its own handler via options. What do you think?

Thanks for feedback.

weavejester commented 6 years ago

I think this is a good idea, though as you mention, the best way forward would be to allow developers to define their own handler through an option.

Do we know that an exception will only be raised if the content is malformed?

LukasRychtecky commented 6 years ago

It's recommended by Muuntaja https://github.com/metosin/muuntaja/blob/8e59542c5e557467de2d0018610c0d0a38d01e87/src/clj/muuntaja/middleware.clj#L17 (it checks for a type of an exception).