0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 16:00:19 -05:00

misc(backend): error middleware reorganization

This commit is contained in:
Andrey Antukh 2019-06-15 18:10:18 +02:00
parent 7400f0f946
commit 57568f9614
3 changed files with 16 additions and 18 deletions

View file

@ -13,7 +13,7 @@
metosin/reitit-ring {:mvn/version "0.3.7"}
metosin/reitit-middleware {:mvn/version "0.3.7"}
metosin/reitit-spec {:mvn/version "0.3.7"}
;; metosin/reitit-dev {:mvn/version "0.3.7"}
metosin/reitit-dev {:mvn/version "0.3.7"}
org.jsoup/jsoup {:mvn/version "1.10.2"}
hiccup/hiccup {:mvn/version "1.0.5"}

View file

@ -5,26 +5,18 @@
;; Copyright (c) 20162019 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.api.errors
"A errors handling for api."
(:require [reitit.ring.middleware.exception :as exception]))
"A errors handling for api.")
(defmulti handle-exception #(:type (ex-data %)))
(defmethod handle-exception :validation
[err]
;; (println "\n*********** stack trace ***********")
;; (.printStackTrace err)
;; (println "\n********* end stack trace *********")
(let [response (ex-data err)]
{:status 400
:body response}))
(defmethod handle-exception :default
[err]
;; (println "\n*********** stack trace ***********")
;; (.printStackTrace err)
;; (println "\n********* end stack trace *********")
(let [response (ex-data err)]
{:status 500
:body response}))
@ -78,9 +70,3 @@
(.printStackTrace error)
(println "\n********* end stack trace *********")
(handler error request))
(def exception-middleware
(exception/create-exception-middleware
(assoc exception/default-handlers
::exception/default errors-handler
::exception/wrap wrap-print-errors)))

View file

@ -8,9 +8,11 @@
(:require [muuntaja.core :as m]
[promesa.core :as p]
[reitit.core :as rc]
[reitit.dev.pretty :as pretty]
[reitit.ring.middleware.multipart :as multipart]
[reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.ring.middleware.parameters :as parameters]
[reitit.ring.middleware.exception :as exception]
[ring.middleware.session :refer [wrap-session]]
[ring.middleware.session.cookie :refer [cookie-store]]
[struct.core :as st]
@ -68,7 +70,7 @@
(fn [req key spec]
(let [[errors, result] ((:fn spec) (get req key))]
(if errors
(ex/raise :type :parameters-validation
(ex/raise :type :validation
:code (:key spec)
:context errors
:value (get req key)
@ -91,6 +93,8 @@
{:name ::parameters-validation-middleware
:compile compile}))
(def ^:private session-middleware
(let [options {:store (cookie-store {:key "a 16-byte secret"})
:cookie-name "session"
@ -107,12 +111,20 @@
;; "content-type"
;; "authorization"])})
(def ^:private exception-middleware
(exception/create-exception-middleware
(assoc exception/default-handlers
::exception/default api-errors/errors-handler
::exception/wrap api-errors/wrap-print-errors)))
(def ^:private muuntaja-instance
(m/create (update-in m/default-options [:formats "application/transit+json"]
merge {:encoder-opts {:handlers t/+write-handlers+}
:decoder-opts {:handlers t/+read-handlers+}})))
(def router-options
{;;:reitit.middleware/transform dev/print-request-diffs
:exception pretty/exception
:data {:muuntaja muuntaja-instance
:middleware [session-middleware
parameters/parameters-middleware
@ -122,7 +134,7 @@
;; encoding response body
muuntaja/format-response-middleware
;; exception handling
api-errors/exception-middleware
exception-middleware
;; decoding request body
muuntaja/format-request-middleware
;; validation