Skip to content

Commit 05025a6

Browse files
Fix #8 unhandled exception when parsing malformed body
1 parent 52cd93c commit 05025a6

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/duct/handler/static.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
(defmethod ig/init-key ::bad-request [_ response]
5050
(make-handler (assoc response :status 400)))
5151

52+
(defmethod ig/init-key ::bad-request-malformed [_ response]
53+
(let [handler (make-handler (assoc response :status 400))]
54+
(fn [_ _ request]
55+
(handler request))))
56+
5257
(defmethod ig/init-key ::not-found [_ response]
5358
(make-handler (assoc response :status 404)))
5459

src/duct/middleware/web.clj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,10 @@
123123
(merge-with deep-merge a b)
124124
b))
125125

126-
(defmethod ig/init-key ::format [_ options]
127-
#(mm/wrap-format % (deep-merge mc/default-options options)))
126+
(defmethod ig/init-key ::format [_ {:keys [malformed-handler]
127+
:as options
128+
:or {malformed-handler
129+
(ig/ref :duct.handler.static/bad-request-malformed)}}]
130+
#(mm/wrap-exception
131+
(mm/wrap-format % (deep-merge mc/default-options options))
132+
malformed-handler))

src/duct/module/web.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns duct.module.web
22
(:require [clojure.java.io :as io]
33
[clojure.string :as str]
4+
[cheshire.core :as cheshire]
45
[duct.core :as core]
56
[duct.core.env :as env]
67
[duct.core.merge :as merge]
@@ -70,6 +71,7 @@
7071

7172
(def ^:private base-config
7273
{:duct.handler.static/bad-request (plaintext-response "Bad Request")
74+
:duct.handler.static/bad-request-malformed (plaintext-response "Bad Request Malformed")
7375
:duct.handler.static/not-found (plaintext-response "Not Found")
7476
:duct.handler.static/method-not-allowed (plaintext-response "Method Not Allowed")
7577
:duct.handler.static/internal-server-error (plaintext-response "Internal Server Error")
@@ -80,6 +82,8 @@
8082

8183
(def ^:private api-config
8284
{:duct.handler.static/bad-request {:body ^:displace {:error :bad-request}}
85+
:duct.handler.static/bad-request-malformed {:body (cheshire/generate-string {:error "Malformed request body"})
86+
:headers {"Content-Type" "application/json"}}
8387
:duct.handler.static/not-found {:body ^:displace {:error :not-found}}
8488
:duct.handler.static/method-not-allowed {:body ^:displace {:error :method-not-allowed}}
8589
:duct.handler.static/internal-server-error
@@ -113,6 +117,7 @@
113117

114118
(defn- site-config [project-ns]
115119
{:duct.handler.static/bad-request (html-response error-400)
120+
:duct.handler.static/bad-request-malformed (html-response error-400)
116121
:duct.handler.static/not-found (html-response error-404)
117122
:duct.handler.static/method-not-allowed (html-response error-405)
118123
:duct.handler.static/internal-server-error (html-response error-500)

0 commit comments

Comments
 (0)