duct-framework / duct-framework/module.web
Uncaught exception when a request body is not valid e.g. JSON
Nobody has claimed this yet.
- Dominant language
- Clojure
- Stars
- 22
- Forks
- 22
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at duct.middleware.web/format and inspect how muuntaja.middleware/wrap-format is applied and how options are passed through. Compare the available wrap-exception behavior, then determine how a custom handler should be configured via options; done means malformed request bodies return a handled 400 response instead of an uncaught exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- clojure
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100