Custom responses for error statuses

next
Marcin Kulik 7 years ago
parent 6769b08818
commit 800f466fa9

@ -1,6 +1,7 @@
(ns asciinema.yada
(:require [clojure.java.io :as io]
[taoensso.timbre :as log]
[yada.status :as status]
[yada.yada :as yada]))
(def not-found-model
@ -14,13 +15,23 @@
"text/html" (io/input-stream (io/resource "asciinema/errors/404.html"))
"Not found")))})
(defn error-response [ctx]
(let [status (-> ctx :response :status)
status-name (get-in status/status [status :name])]
(case (yada/content-type ctx)
"text/html" (str "<html><body><h1>" status-name "</h1></body></html>")
status-name)))
(defn logger [ctx]
(when-let [error (:error ctx)]
(when (not= (-> ctx :response :status) 404)
(log/error error))))
(defn resource [model]
(-> model
(assoc :logger logger)
(update-in [:responses 404] #(or % not-found-model))
yada/resource))
(let [error-statuses (set (concat (range 400 404) (range 405 600) ))]
(-> model
(assoc :logger logger)
(update-in [:responses 404] #(or % not-found-model))
(update-in [:responses error-statuses] #(or % {:produces #{"text/html" "text/plain"}
:response error-response}))
yada/resource)))

Loading…
Cancel
Save