diff --git a/src/duct/handler/static.clj b/src/duct/handler/static.clj index 6a00a3b..a841048 100644 --- a/src/duct/handler/static.clj +++ b/src/duct/handler/static.clj @@ -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))) diff --git a/src/duct/middleware/web.clj b/src/duct/middleware/web.clj index c892c28..845f367 100644 --- a/src/duct/middleware/web.clj +++ b/src/duct/middleware/web.clj @@ -123,5 +123,10 @@ (merge-with deep-merge a b) b)) -(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 + :or {malformed-handler + (ig/ref :duct.handler.static/bad-request-malformed)}}] + #(mm/wrap-exception + (mm/wrap-format % (deep-merge mc/default-options options)) + malformed-handler)) diff --git a/src/duct/module/web.clj b/src/duct/module/web.clj index 0801a38..897683b 100644 --- a/src/duct/module/web.clj +++ b/src/duct/module/web.clj @@ -1,6 +1,7 @@ (ns duct.module.web (:require [clojure.java.io :as io] [clojure.string :as str] + [cheshire.core :as cheshire] [duct.core :as core] [duct.core.env :as env] [duct.core.merge :as merge] @@ -70,6 +71,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") @@ -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"}) + :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 @@ -113,6 +117,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)