-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #8 unhandled exception when parsing malformed body #9
base: master
Are you sure you want to change the base?
Fix #8 unhandled exception when parsing malformed body #9
Conversation
@@ -49,6 +49,11 @@ | |||
(defmethod ig/init-key ::bad-request [_ response] | |||
(make-handler (assoc response :status 400))) | |||
|
|||
(defmethod ig/init-key ::bad-request-malformed [_ response] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New static handler
src/duct/middleware/web.clj
Outdated
@@ -124,4 +124,8 @@ | |||
b)) | |||
|
|||
(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than use or
, supply a default value instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I'll try it.
src/duct/module/web.clj
Outdated
@@ -80,6 +81,8 @@ | |||
|
|||
(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded JSON, I don't want to add another JSON library as dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably yes, Muuntaja uses CHeshire itself.
src/duct/middleware/web.clj
Outdated
@@ -124,4 +124,8 @@ | |||
b)) | |||
|
|||
(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than use or
, supply a default value instead.
src/duct/middleware/web.clj
Outdated
@@ -124,4 +124,8 @@ | |||
b)) | |||
|
|||
(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, I 've created the PR from 52cd93c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I rebased from master. Could you suggest how to solve it without ig/ref
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -80,6 +82,8 @@ | |||
|
|||
(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generating JSON via Cheshire
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it this OK, or you meant it in a different way?
05025a6
to
76a7e98
Compare
src/duct/middleware/web.clj
Outdated
(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.