Fix #8 unhandled exception when parsing malformed body#9
Fix #8 unhandled exception when parsing malformed body#9LukasRychtecky wants to merge 1 commit intoduct-framework:masterfrom
Conversation
| (defmethod ig/init-key ::bad-request [_ response] | ||
| (make-handler (assoc response :status 400))) | ||
|
|
||
| (defmethod ig/init-key ::bad-request-malformed [_ response] |
src/duct/middleware/web.clj
Outdated
|
|
||
| (defmethod ig/init-key ::format [_ options] | ||
| #(mm/wrap-format % (deep-merge mc/default-options options))) | ||
| (let [malformed-handler (or (:malformed-handler options) |
There was a problem hiding this comment.
A default handler can be overridden by config:
{:duct.middleware.web/format {:malformed-handler #ig/ref :myapp.malformed-handler}There was a problem hiding this comment.
Rather than use or, supply a default value instead.
src/duct/module/web.clj
Outdated
|
|
||
| (def ^:private api-config | ||
| {:duct.handler.static/bad-request {:body ^:displace {:error :bad-request}} | ||
| :duct.handler.static/bad-request-malformed {:body "{\"error\": \"Malformed request body\"}" |
There was a problem hiding this comment.
Hardcoded JSON, I don't want to add another JSON library as dependency.
There was a problem hiding this comment.
Since all the other handlers are data, rather than hardcoded, I don't think we should hardcode the JSON. Can we let Muuntaja handle the formatting of the error message?
There was a problem hiding this comment.
Probably yes, Muuntaja uses CHeshire itself.
src/duct/middleware/web.clj
Outdated
|
|
||
| (defmethod ig/init-key ::format [_ options] | ||
| #(mm/wrap-format % (deep-merge mc/default-options options))) | ||
| (let [malformed-handler (or (:malformed-handler options) |
There was a problem hiding this comment.
Rather than use or, supply a default value instead.
src/duct/middleware/web.clj
Outdated
| (defmethod ig/init-key ::format [_ options] | ||
| #(mm/wrap-format % (deep-merge mc/default-options options))) | ||
| (let [malformed-handler (or (:malformed-handler options) | ||
| (ig/ref :duct.handler.static/bad-request-malformed))] |
There was a problem hiding this comment.
Supplying a reference here won't work. In the current alpha of Duct there is prep-key for such eventualities, but in the current stable version it needs to be hardcoded or set by a module.
There was a problem hiding this comment.
OK I rebased from master. Could you suggest how to solve it without ig/ref?
There was a problem hiding this comment.
You should add the ref via the duct.module.web/api module, rather than the options of the ig/init-key method.
cee1a6d to
05025a6
Compare
src/duct/module/web.clj
Outdated
|
|
||
| (def ^:private api-config | ||
| {:duct.handler.static/bad-request {:body ^:displace {:error :bad-request}} | ||
| :duct.handler.static/bad-request-malformed {:body (cheshire/generate-string {:error "Malformed request body"}) |
There was a problem hiding this comment.
Generating JSON via Cheshire
There was a problem hiding this comment.
Wouldn't it be better to do this as part of the error handling? Otherwise :duct.handler.static/bad-request-malformed is treated differently from all the other handlers.
| (defmethod ig/init-key ::format [_ options] | ||
| #(mm/wrap-format % (deep-merge mc/default-options options))) | ||
| (defmethod ig/init-key ::format [_ {:keys [malformed-handler] | ||
| :as options |
There was a problem hiding this comment.
Is it this OK, or you meant it in a different way?
05025a6 to
76a7e98
Compare
| (let [handler (or (:malformed-handler options) | ||
| (:default-malformed-handler options))] | ||
| (-> (handler error content-type request) | ||
| (update :body cheshire/generate-string) |
There was a problem hiding this comment.
Processing a response in a handler as proposed.
76a7e98 to
a029366
Compare
a029366 to
3a95d77
Compare
|
Thanks for the update! There's still a couple of issues:
Instead, attach the Muuntaja error and content type as namespaced keys onto the request map (e.g. |
|
@weavejester thanks for feedback. Sorry for a long running PR, but I've been really busy in last weeks. I expect I will finish it soon. |
This is just a preview, I want to sure about the code before writting tests.