Skip to content

Commit 48d8872

Browse files
committed
Add '406 Not Acceptable' error handler
1 parent 7754295 commit 48d8872

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en" class="error-page">
3+
<head>
4+
<title>Server Error</title>
5+
<link rel="stylesheet" href="/assets/normalize.css/normalize.css">
6+
<link rel="stylesheet" href="/css/site.css">
7+
</head>
8+
<body>
9+
<h1>Not Acceptable</h1>
10+
<h2>Content negotiation for this resource has failed.</h2>
11+
</body>
12+
</html>

src/duct/handler/static.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@
5555
(defmethod ig/init-key ::method-not-allowed [_ response]
5656
(make-handler (assoc response :status 405)))
5757

58+
(defmethod ig/init-key ::not-acceptable [_ response]
59+
(make-handler (assoc response :status 406)))
60+
5861
(defmethod ig/init-key ::internal-server-error [_ response]
5962
(make-handler (assoc response :status 500)))

src/duct/module/web.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
:main (ig/ref :duct.middleware.web/hide-errors))]
3232
:default-handler
3333
{:not-found ~(ig/ref :duct.handler.static/not-found)
34-
:method-not-allowed ~(ig/ref :duct.handler.static/method-not-allowed)}}
34+
:method-not-allowed ~(ig/ref :duct.handler.static/method-not-allowed)
35+
:not-acceptable ~(ig/ref :duct.handler.static/not-acceptable)}}
3536

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

87+
:duct.handler.static/not-acceptable
88+
~(cond
89+
site? (html-response (io/resource "duct/module/web/errors/406.html"))
90+
api? {:body {:error :not-acceptable}}
91+
:else (plaintext-response "Not Acceptable"))
92+
8693
:duct.handler.static/internal-server-error
8794
~(cond
8895
site? (html-response (io/resource "duct/module/web/errors/500.html"))

test/duct/module/web_test.clj

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
{:not-found
1717
(ig/ref :duct.handler.static/not-found)
1818
:method-not-allowed
19-
(ig/ref :duct.handler.static/method-not-allowed)}}
19+
(ig/ref :duct.handler.static/method-not-allowed)
20+
:not-acceptable
21+
(ig/ref :duct.handler.static/not-acceptable)}}
2022
:duct.middleware.web/defaults
2123
{:params {:urlencoded true, :keywordize true}
2224
:responses {:not-modified-responses true
@@ -36,6 +38,9 @@
3638
:duct.handler.static/method-not-allowed
3739
{:headers {"Content-Type" "text/plain; charset=UTF-8"}
3840
:body "Method Not Allowed"}
41+
:duct.handler.static/not-acceptable
42+
{:headers {"Content-Type" "text/plain; charset=UTF-8"}
43+
:body "Not Acceptable"}
3944
:duct.handler.static/internal-server-error
4045
{:headers {"Content-Type" "text/plain; charset=UTF-8"}
4146
:body "Internal Server Error"}
@@ -60,7 +65,9 @@
6065
{:not-found
6166
(ig/ref :duct.handler.static/not-found)
6267
:method-not-allowed
63-
(ig/ref :duct.handler.static/method-not-allowed)}}
68+
(ig/ref :duct.handler.static/method-not-allowed)
69+
:not-acceptable
70+
(ig/ref :duct.handler.static/not-acceptable)}}
6471
:duct.middleware.web/defaults
6572
{:params {:urlencoded true, :keywordize true}
6673
:responses {:not-modified-responses true
@@ -77,6 +84,8 @@
7784
{:body {:error :not-found}}
7885
:duct.handler.static/method-not-allowed
7986
{:body {:error :method-not-allowed}}
87+
:duct.handler.static/not-acceptable
88+
{:body {:error :not-acceptable}}
8089
:duct.handler.static/internal-server-error
8190
{:headers {"Content-Type" "application/json"}
8291
:body (io/resource "duct/module/web/errors/500.json")}
@@ -102,7 +111,9 @@
102111
{:not-found
103112
(ig/ref :duct.handler.static/not-found)
104113
:method-not-allowed
105-
(ig/ref :duct.handler.static/method-not-allowed)}}
114+
(ig/ref :duct.handler.static/method-not-allowed)
115+
:not-acceptable
116+
(ig/ref :duct.handler.static/not-acceptable)}}
106117
:duct.middleware.web/defaults
107118
{:params {:urlencoded true
108119
:multipart true
@@ -135,6 +146,9 @@
135146
:duct.handler.static/method-not-allowed
136147
{:headers {"Content-Type" "text/html; charset=UTF-8"}
137148
:body (io/resource "duct/module/web/errors/405.html")}
149+
:duct.handler.static/not-acceptable
150+
{:headers {"Content-Type" "text/html; charset=UTF-8"}
151+
:body (io/resource "duct/module/web/errors/406.html")}
138152
:duct.handler.static/internal-server-error
139153
{:headers {"Content-Type" "text/html; charset=UTF-8"}
140154
:body (io/resource "duct/module/web/errors/500.html")}

0 commit comments

Comments
 (0)