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