Skip to content

Fix #8 unhandled exception when parsing malformed body #9

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
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

(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
16 changes: 13 additions & 3 deletions src/duct/middleware/web.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns duct.middleware.web
(:require [duct.logger :as logger]
(:require [cheshire.core :as cheshire]
[duct.logger :as logger]
[integrant.core :as ig]
[muuntaja.core :as mc]
[muuntaja.middleware :as mm]
Expand Down Expand Up @@ -129,5 +130,14 @@
(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
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?

:or {malformed-handler
(fn [error content-type request]
(let [handler (:malformed-handler options)]
(-> (handler error content-type request)
(update :body cheshire/generate-string)
(assoc-in [:headers "Content-Type"] "application/json"))))}}]
#(mm/wrap-exception
(mm/wrap-format % (deep-merge mc/default-options options))
malformed-handler))
5 changes: 4 additions & 1 deletion 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,11 +81,12 @@

(def ^:private api-config
{:duct.handler.static/bad-request {:body ^:displace {:error :bad-request}}
:duct.handler.static/bad-request-malformed {:body ^:displace {:error :malformed-request-body}}
: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
{:body ^:displace {:error :internal-server-error}}
:duct.middleware.web/format {}
:duct.middleware.web/format {:malformed-handler (ig/ref :duct.handler.static/bad-request-malformed)}
:duct.middleware.web/defaults base-ring-defaults
:duct.core/handler
{:middleware ^:distinct [(ig/ref :duct.middleware.web/not-found)
Expand Down Expand Up @@ -113,6 +115,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
11 changes: 10 additions & 1 deletion test/duct/module/web_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
:duct.handler.static/bad-request
{:headers {"Content-Type" "text/plain; charset=UTF-8"}
:body "Bad Request"}
:duct.handler.static/bad-request-malformed
{:headers {"Content-Type" "text/plain; charset=UTF-8"}
:body "Bad Request Malformed"}
:duct.handler.static/not-found
{:headers {"Content-Type" "text/plain; charset=UTF-8"}
:body "Not Found"}
Expand Down Expand Up @@ -102,13 +105,16 @@
:logger (ig/ref :duct/logger)}
:duct.handler.static/bad-request
{:body {:error :bad-request}}
:duct.handler.static/bad-request-malformed
{:body {:error :malformed-request-body}}
:duct.handler.static/not-found
{:body {:error :not-found}}
:duct.handler.static/method-not-allowed
{:body {:error :method-not-allowed}}
:duct.handler.static/internal-server-error
{:body {:error :internal-server-error}}
:duct.middleware.web/format {}
:duct.middleware.web/format
{:malformed-handler (ig/ref :duct.handler.static/bad-request-malformed)}
:duct.middleware.web/stacktrace {}
:duct.middleware.web/hide-errors
{:error-handler (ig/ref :duct.handler.static/internal-server-error)}
Expand Down Expand Up @@ -155,6 +161,9 @@
:duct.middleware.web/webjars {}
:duct.middleware.web/stacktrace {}
:duct.handler.static/bad-request
{:headers {"Content-Type" "text/html; charset=UTF-8"}
:body (io/resource "duct/module/web/errors/400.html")}
:duct.handler.static/bad-request-malformed
{:headers {"Content-Type" "text/html; charset=UTF-8"}
:body (io/resource "duct/module/web/errors/400.html")}
:duct.handler.static/not-found
Expand Down