-
Notifications
You must be signed in to change notification settings - Fork 64
/
middleware.clj
33 lines (30 loc) · 1.23 KB
/
middleware.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(ns ziggurat.server.middleware
(:require [clojure.string :as str]
[camel-snake-kebab.core :as csk]
[cheshire.core :as json]
[clj-stacktrace.repl :as st]
[ring.util.response :as ring-resp]
[sentry-clj.async :as sentry]
[ziggurat.sentry :refer [sentry-reporter]]
[ziggurat.util.map :as umap]))
(defn wrap-default-content-type-json [handler]
(fn [request]
(let [response (handler request)
content-type (ring-resp/get-header response "content-type")]
(if (or (nil? content-type) (str/starts-with? content-type "application/octet-stream"))
(ring-resp/content-type response "application/json; charset=utf-8")
response))))
(defn wrap-hyphenate [handler & args]
(fn [request]
(let [response
(handler (update request
:params
#(umap/nested-map-keys (fn [k] (apply csk/->kebab-case-keyword k args)) %)))]
response)))
(defn wrap-errors [handler]
(fn [request]
(try
(handler request)
(catch Exception ex
(sentry/report-error sentry-reporter ex "Uncaught error in server")
{:status 500 :body (json/encode {:Error (st/pst-str ex)})}))))