Skip to content

Commit

Permalink
Add '406 Not Acceptable' error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Nov 16, 2024
1 parent 7754295 commit 48d8872
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
12 changes: 12 additions & 0 deletions resources/duct/module/web/errors/406.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en" class="error-page">
<head>
<title>Server Error</title>
<link rel="stylesheet" href="/assets/normalize.css/normalize.css">
<link rel="stylesheet" href="/css/site.css">
</head>
<body>
<h1>Not Acceptable</h1>
<h2>Content negotiation for this resource has failed.</h2>
</body>
</html>
3 changes: 3 additions & 0 deletions src/duct/handler/static.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@
(defmethod ig/init-key ::method-not-allowed [_ response]
(make-handler (assoc response :status 405)))

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

(defmethod ig/init-key ::internal-server-error [_ response]
(make-handler (assoc response :status 500)))
9 changes: 8 additions & 1 deletion src/duct/module/web.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
:main (ig/ref :duct.middleware.web/hide-errors))]
:default-handler
{:not-found ~(ig/ref :duct.handler.static/not-found)
:method-not-allowed ~(ig/ref :duct.handler.static/method-not-allowed)}}
:method-not-allowed ~(ig/ref :duct.handler.static/method-not-allowed)
:not-acceptable ~(ig/ref :duct.handler.static/not-acceptable)}}

:duct.middleware.web/defaults
~(if site?
Expand Down Expand Up @@ -83,6 +84,12 @@
api? {:body {:error :method-not-allowed}}
:else (plaintext-response "Method Not Allowed"))

:duct.handler.static/not-acceptable
~(cond
site? (html-response (io/resource "duct/module/web/errors/406.html"))
api? {:body {:error :not-acceptable}}
:else (plaintext-response "Not Acceptable"))

:duct.handler.static/internal-server-error
~(cond
site? (html-response (io/resource "duct/module/web/errors/500.html"))
Expand Down
20 changes: 17 additions & 3 deletions test/duct/module/web_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
{:not-found
(ig/ref :duct.handler.static/not-found)
:method-not-allowed
(ig/ref :duct.handler.static/method-not-allowed)}}
(ig/ref :duct.handler.static/method-not-allowed)
:not-acceptable
(ig/ref :duct.handler.static/not-acceptable)}}
:duct.middleware.web/defaults
{:params {:urlencoded true, :keywordize true}
:responses {:not-modified-responses true
Expand All @@ -36,6 +38,9 @@
:duct.handler.static/method-not-allowed
{:headers {"Content-Type" "text/plain; charset=UTF-8"}
:body "Method Not Allowed"}
:duct.handler.static/not-acceptable
{:headers {"Content-Type" "text/plain; charset=UTF-8"}
:body "Not Acceptable"}
:duct.handler.static/internal-server-error
{:headers {"Content-Type" "text/plain; charset=UTF-8"}
:body "Internal Server Error"}
Expand All @@ -60,7 +65,9 @@
{:not-found
(ig/ref :duct.handler.static/not-found)
:method-not-allowed
(ig/ref :duct.handler.static/method-not-allowed)}}
(ig/ref :duct.handler.static/method-not-allowed)
:not-acceptable
(ig/ref :duct.handler.static/not-acceptable)}}
:duct.middleware.web/defaults
{:params {:urlencoded true, :keywordize true}
:responses {:not-modified-responses true
Expand All @@ -77,6 +84,8 @@
{:body {:error :not-found}}
:duct.handler.static/method-not-allowed
{:body {:error :method-not-allowed}}
:duct.handler.static/not-acceptable
{:body {:error :not-acceptable}}
:duct.handler.static/internal-server-error
{:headers {"Content-Type" "application/json"}
:body (io/resource "duct/module/web/errors/500.json")}
Expand All @@ -102,7 +111,9 @@
{:not-found
(ig/ref :duct.handler.static/not-found)
:method-not-allowed
(ig/ref :duct.handler.static/method-not-allowed)}}
(ig/ref :duct.handler.static/method-not-allowed)
:not-acceptable
(ig/ref :duct.handler.static/not-acceptable)}}
:duct.middleware.web/defaults
{:params {:urlencoded true
:multipart true
Expand Down Expand Up @@ -135,6 +146,9 @@
:duct.handler.static/method-not-allowed
{:headers {"Content-Type" "text/html; charset=UTF-8"}
:body (io/resource "duct/module/web/errors/405.html")}
:duct.handler.static/not-acceptable
{:headers {"Content-Type" "text/html; charset=UTF-8"}
:body (io/resource "duct/module/web/errors/406.html")}
:duct.handler.static/internal-server-error
{:headers {"Content-Type" "text/html; charset=UTF-8"}
:body (io/resource "duct/module/web/errors/500.html")}
Expand Down

0 comments on commit 48d8872

Please sign in to comment.