Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

LukasRychtecky
Copy link

This is just a preview, I want to sure about the code before writting tests.

@@ -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]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New static handler

@@ -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)
Copy link
Author

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}

Copy link
Contributor

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.

Copy link
Author

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.

@@ -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\"}"
Copy link
Author

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.

Copy link
Contributor

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?

Copy link
Author

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.

@@ -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)
Copy link
Contributor

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.

@@ -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))]
Copy link
Contributor

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.

Copy link
Author

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

Copy link
Author

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?

Copy link
Contributor

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.

@LukasRychtecky LukasRychtecky force-pushed the 8_Malformed-body-fix branch from cee1a6d to 05025a6 Compare May 25, 2018 07:45
@@ -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"})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generating JSON via Cheshire

Copy link
Contributor

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
Copy link
Author

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?

@LukasRychtecky LukasRychtecky force-pushed the 8_Malformed-body-fix branch from 05025a6 to 76a7e98 Compare June 4, 2018 17:29
(let [handler (or (:malformed-handler options)
(:default-malformed-handler options))]
(-> (handler error content-type request)
(update :body cheshire/generate-string)
Copy link
Author

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.

@LukasRychtecky LukasRychtecky changed the title NOT-TO-MERGE: Fix #8 unhandled exception when parsing malformed body Fix #8 unhandled exception when parsing malformed body Aug 30, 2018
@weavejester
Copy link
Contributor

Thanks for the update! There's still a couple of issues:

  1. We shouldn't specify the default handler function in both the module and the middleware.
  2. The handler function you've added isn't a valid Ring handler function.

Instead, attach the Muuntaja error and content type as namespaced keys onto the request map (e.g. :muuntaja/error and muuntaja/content-type), and remove the default error handler. If the error handler is missing, don't wrap the exception and allow it to be thrown.

@LukasRychtecky
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants