mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21:11 +00:00
cleanup multipart
This commit is contained in:
parent
8156922ebc
commit
62cdfa2c52
3 changed files with 91 additions and 69 deletions
|
|
@ -2,26 +2,6 @@
|
||||||
(:require [reitit.coercion :as coercion]
|
(:require [reitit.coercion :as coercion]
|
||||||
[reitit.ring :as ring]))
|
[reitit.ring :as ring]))
|
||||||
|
|
||||||
(defn default-handler [^Exception e _ _]
|
|
||||||
{:status 500
|
|
||||||
:body {:type "exception"
|
|
||||||
:class (.getName (.getClass e))}})
|
|
||||||
|
|
||||||
(defn coercion-handler [status]
|
|
||||||
(fn [_ data _]
|
|
||||||
{:status status
|
|
||||||
:body (coercion/encode-error data)}))
|
|
||||||
|
|
||||||
(defn http-response-handler
|
|
||||||
"reads response from ex-data :response"
|
|
||||||
[_ {:keys [response]} _]
|
|
||||||
response)
|
|
||||||
|
|
||||||
(defn request-parsing-handler [_ {:keys [format]} _]
|
|
||||||
{:status 400
|
|
||||||
:headers {"Content-Type" "text/plain"}
|
|
||||||
:body (str "Malformed " (pr-str format) " request.")})
|
|
||||||
|
|
||||||
(defn- super-classes [^Class k]
|
(defn- super-classes [^Class k]
|
||||||
(loop [sk (.getSuperclass k), ks []]
|
(loop [sk (.getSuperclass k), ks []]
|
||||||
(if-not (= sk Object)
|
(if-not (= sk Object)
|
||||||
|
|
@ -29,7 +9,7 @@
|
||||||
ks)))
|
ks)))
|
||||||
|
|
||||||
(defn- call-error-handler [handlers error request]
|
(defn- call-error-handler [handlers error request]
|
||||||
(let [{:keys [type] :as data} (ex-data error)
|
(let [type (:type (ex-data error))
|
||||||
ex-class (class error)
|
ex-class (class error)
|
||||||
error-handler (or (get handlers type)
|
error-handler (or (get handlers type)
|
||||||
(get handlers ex-class)
|
(get handlers ex-class)
|
||||||
|
|
@ -39,8 +19,8 @@
|
||||||
(some
|
(some
|
||||||
(partial get handlers)
|
(partial get handlers)
|
||||||
(super-classes ex-class))
|
(super-classes ex-class))
|
||||||
(get handlers ::default default-handler))]
|
(get handlers ::default))]
|
||||||
(error-handler error data request)))
|
(error-handler error request)))
|
||||||
|
|
||||||
(defn- on-exception [handlers e request respond raise]
|
(defn- on-exception [handlers e request respond raise]
|
||||||
(try
|
(try
|
||||||
|
|
@ -48,44 +28,83 @@
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
(raise e))))
|
(raise e))))
|
||||||
|
|
||||||
(defn- wrap [options]
|
;;
|
||||||
|
;; handlers
|
||||||
|
;;
|
||||||
|
|
||||||
|
(defn default-handler
|
||||||
|
"Default safe handler for any exception."
|
||||||
|
[^Exception e _]
|
||||||
|
{:status 500
|
||||||
|
:body {:type "exception"
|
||||||
|
:class (.getName (.getClass e))}})
|
||||||
|
|
||||||
|
(defn create-coercion-handler
|
||||||
|
"Creates a coercion exception handler."
|
||||||
|
[status]
|
||||||
|
(fn [e _]
|
||||||
|
{:status status
|
||||||
|
:body (coercion/encode-error (ex-data e))}))
|
||||||
|
|
||||||
|
(defn http-response-handler
|
||||||
|
"Reads response from Exception ex-data :response"
|
||||||
|
[e _]
|
||||||
|
(-> e ex-data :response))
|
||||||
|
|
||||||
|
(defn request-parsing-handler [e _]
|
||||||
|
{:status 400
|
||||||
|
:headers {"Content-Type" "text/plain"}
|
||||||
|
:body (str "Malformed " (-> e ex-data :format pr-str) " request.")})
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; public api
|
||||||
|
;;
|
||||||
|
|
||||||
|
(def default-handlers
|
||||||
|
{::default default-handler
|
||||||
|
::ring/response http-response-handler
|
||||||
|
:muuntaja/decode request-parsing-handler
|
||||||
|
::coercion/request-coercion (create-coercion-handler 400)
|
||||||
|
::coercion/response-coercion (create-coercion-handler 500)})
|
||||||
|
|
||||||
|
(defn wrap-exception [handlers]
|
||||||
(fn [handler]
|
(fn [handler]
|
||||||
(fn
|
(fn
|
||||||
([request]
|
([request]
|
||||||
(try
|
(try
|
||||||
(handler request)
|
(handler request)
|
||||||
(catch Throwable e
|
(catch Throwable e
|
||||||
(on-exception options e request identity #(throw %)))))
|
(on-exception handlers e request identity #(throw %)))))
|
||||||
([request respond raise]
|
([request respond raise]
|
||||||
(try
|
(try
|
||||||
(handler request respond (fn [e] (on-exception options e request respond raise)))
|
(handler request respond (fn [e] (on-exception handlers e request respond raise)))
|
||||||
(catch Throwable e
|
(catch Throwable e
|
||||||
(on-exception options e request respond raise)))))))
|
(on-exception handlers e request respond raise)))))))
|
||||||
|
|
||||||
;;
|
|
||||||
;; public api
|
|
||||||
;;
|
|
||||||
|
|
||||||
(def default-options
|
|
||||||
{::default default-handler
|
|
||||||
::ring/response http-response-handler
|
|
||||||
:muuntaja/decode request-parsing-handler
|
|
||||||
::coercion/request-coercion (coercion-handler 400)
|
|
||||||
::coercion/response-coercion (coercion-handler 500)})
|
|
||||||
|
|
||||||
(def exception-middleware
|
(def exception-middleware
|
||||||
"Catches all exceptions and looks up a exception handler:
|
"Middleware that catches all exceptions and looks up a exception handler
|
||||||
|
from a [[default-handlers]] map in the lookup order:
|
||||||
|
|
||||||
1) `:type` of ex-data
|
1) `:type` of ex-data
|
||||||
2) Class of Exception
|
2) Class of Exception
|
||||||
3) descadents `:type` of ex-data
|
3) descadents `:type` of ex-data
|
||||||
4) Super Classes of Exception
|
4) Super Classes of Exception
|
||||||
5) The ::default handler"
|
5) The ::default handler"
|
||||||
{:name ::exception
|
{:name ::exception
|
||||||
:wrap (wrap default-options)})
|
:wrap (wrap-exception default-handlers)})
|
||||||
|
|
||||||
(defn create-exception-middleware
|
(defn create-exception-middleware
|
||||||
|
"Creates a middleware that catches all exceptions and looks up a exception handler
|
||||||
|
from a given map of `handlers` with keyword or Exception class as keys and a 2-arity
|
||||||
|
Exception handler function as values.
|
||||||
|
|
||||||
|
1) `:type` of ex-data
|
||||||
|
2) Class of Exception
|
||||||
|
3) descadents `:type` of ex-data
|
||||||
|
4) Super Classes of Exception
|
||||||
|
5) The ::default handler"
|
||||||
([]
|
([]
|
||||||
(create-exception-middleware default-options))
|
(create-exception-middleware default-handlers))
|
||||||
([options]
|
([handlers]
|
||||||
{:name ::exception
|
{:name ::exception
|
||||||
:wrap (wrap options)}))
|
:wrap (wrap-exception handlers)}))
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,23 @@
|
||||||
(ns reitit.ring.middleware.multipart
|
(ns ^:no-doc reitit.ring.middleware.multipart
|
||||||
(:require [reitit.coercion :as coercion]
|
(:require [reitit.coercion :as coercion]
|
||||||
[ring.middleware.multipart-params :as multipart-params]))
|
[ring.middleware.multipart-params :as multipart-params]))
|
||||||
|
|
||||||
(defn multipart-params
|
(def parameter-coercion
|
||||||
|
{:multipart (coercion/->ParameterCoercion :multipart-params :string true true)})
|
||||||
|
|
||||||
|
(defn coerced-request [request coercers]
|
||||||
|
(if-let [coerced (if coercers (coercion/coerce-request coercers request))]
|
||||||
|
(update request :parameters merge coerced)
|
||||||
|
request))
|
||||||
|
|
||||||
|
(defn create-multipart-middleware
|
||||||
"Creates a Middleware to handle the multipart params, based on
|
"Creates a Middleware to handle the multipart params, based on
|
||||||
ring.middleware.multipart-params, taking same options. Mounts only
|
ring.middleware.multipart-params, taking same options. Mounts only
|
||||||
if endpoint has `[:parameters :multipart]` defined. Publishes coerced
|
if endpoint has `[:parameters :multipart]` defined. Publishes coerced
|
||||||
parameters into `[:parameters :multipart]` under request."
|
parameters into `[:parameters :multipart]` under request."
|
||||||
([]
|
([]
|
||||||
(multipart-params nil))
|
(create-multipart-middleware nil))
|
||||||
([options]
|
([options]
|
||||||
(let [parameter-coercion {:multipart (coercion/->ParameterCoercion :multipart-params :string true true)}
|
|
||||||
coerced-request (fn [request coercers]
|
|
||||||
(if-let [coerced (if coercers (coercion/coerce-request coercers request))]
|
|
||||||
(update request :parameters merge coerced)
|
|
||||||
request))]
|
|
||||||
{:name ::multipart
|
{:name ::multipart
|
||||||
:compile (fn [{:keys [parameters coercion]} opts]
|
:compile (fn [{:keys [parameters coercion]} opts]
|
||||||
(if-let [multipart (:multipart parameters)]
|
(if-let [multipart (:multipart parameters)]
|
||||||
|
|
@ -31,4 +34,4 @@
|
||||||
(-> request
|
(-> request
|
||||||
(multipart-params/multipart-params-request options)
|
(multipart-params/multipart-params-request options)
|
||||||
(coerced-request coercers)
|
(coerced-request coercers)
|
||||||
(handler respond raise))))))))})))
|
(handler respond raise))))))))}))
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
:handler f}]]
|
:handler f}]]
|
||||||
{:data {:middleware [(exception/create-exception-middleware
|
{:data {:middleware [(exception/create-exception-middleware
|
||||||
(merge
|
(merge
|
||||||
exception/default-options
|
exception/default-handlers
|
||||||
{::kikka (constantly {:status 200, :body "kikka"})
|
{::kikka (constantly {:status 200, :body "kikka"})
|
||||||
SQLException (constantly {:status 200, :body "sql"})}))]}})))]
|
SQLException (constantly {:status 200, :body "sql"})}))]}})))]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue