Skip to content

Commit

Permalink
Fix duct-framework#8 unhandled exception when parsing malformed body
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasRychtecky committed May 21, 2018
1 parent 52cd93c commit cee1a6d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/duct/handler/static.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
(let [handler (make-handler (assoc response :status 400))]
(fn [_ _ request]
(handler request))))

(defmethod ig/init-key ::not-found [_ response]
(make-handler (assoc response :status 404)))

Expand Down
6 changes: 5 additions & 1 deletion src/duct/middleware/web.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
#(mm/wrap-exception
(mm/wrap-format % (deep-merge mc/default-options options))
malformed-handler)))
4 changes: 4 additions & 0 deletions src/duct/module/web.clj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

(def ^:private base-config
{:duct.handler.static/bad-request (plaintext-response "Bad Request")
:duct.handler.static/bad-request-malformed (plaintext-response "Bad Request Malformed")
:duct.handler.static/not-found (plaintext-response "Not Found")
:duct.handler.static/method-not-allowed (plaintext-response "Method Not Allowed")
:duct.handler.static/internal-server-error (plaintext-response "Internal Server Error")
Expand All @@ -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\"}"
:headers {"Content-Type" "application/json"}}
:duct.handler.static/not-found {:body ^:displace {:error :not-found}}
:duct.handler.static/method-not-allowed {:body ^:displace {:error :method-not-allowed}}
:duct.handler.static/internal-server-error
Expand Down Expand Up @@ -113,6 +116,7 @@

(defn- site-config [project-ns]
{:duct.handler.static/bad-request (html-response error-400)
:duct.handler.static/bad-request-malformed (html-response error-400)
:duct.handler.static/not-found (html-response error-404)
:duct.handler.static/method-not-allowed (html-response error-405)
:duct.handler.static/internal-server-error (html-response error-500)
Expand Down

0 comments on commit cee1a6d

Please sign in to comment.