mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 16:31:11 +00:00
Works with ClojureScript!
This commit is contained in:
parent
132240b422
commit
becd30386d
4 changed files with 13 additions and 21 deletions
|
|
@ -160,7 +160,7 @@
|
||||||
(handler request #(respond (coerce-response coercers request %)) raise)))))))}))
|
(handler request #(respond (coerce-response coercers request %)) raise)))))))}))
|
||||||
|
|
||||||
(def gen-wrap-coerce-exceptions
|
(def gen-wrap-coerce-exceptions
|
||||||
"Middleare for coercion exception handling.
|
"Middleware for handling coercion exceptions.
|
||||||
Expects a :coercion of type `reitit.coercion.protocol/Coercion`
|
Expects a :coercion of type `reitit.coercion.protocol/Coercion`
|
||||||
and :parameters or :responses from route data, otherwise does not mount."
|
and :parameters or :responses from route data, otherwise does not mount."
|
||||||
(middleware/create
|
(middleware/create
|
||||||
|
|
@ -176,6 +176,6 @@
|
||||||
(handle-coercion-exception e identity #(throw %)))))
|
(handle-coercion-exception e identity #(throw %)))))
|
||||||
([request respond raise]
|
([request respond raise]
|
||||||
(try
|
(try
|
||||||
(handler request respond (fn [e] (handle-coercion-exception e respond raise)))
|
(handler request respond #(handle-coercion-exception % respond raise))
|
||||||
(catch #?(:clj Exception :cljs js/Error) e
|
(catch #?(:clj Exception :cljs js/Error) e
|
||||||
(handle-coercion-exception e respond raise))))))))}))
|
(handle-coercion-exception e respond raise))))))))}))
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,7 @@
|
||||||
[schema-tools.coerce :as stc]
|
[schema-tools.coerce :as stc]
|
||||||
[spec-tools.swagger.core :as swagger]
|
[spec-tools.swagger.core :as swagger]
|
||||||
[clojure.walk :as walk]
|
[clojure.walk :as walk]
|
||||||
[reitit.ring.coercion.protocol :as protocol])
|
[reitit.ring.coercion.protocol :as protocol]))
|
||||||
(:import (schema.core OptionalKey RequiredKey)
|
|
||||||
(schema.utils ValidationError NamedError)))
|
|
||||||
|
|
||||||
(def string-coercion-matcher
|
(def string-coercion-matcher
|
||||||
stc/string-coercion-matcher)
|
stc/string-coercion-matcher)
|
||||||
|
|
@ -26,12 +24,12 @@
|
||||||
(walk/prewalk
|
(walk/prewalk
|
||||||
(fn [x]
|
(fn [x]
|
||||||
(cond
|
(cond
|
||||||
(class? x) (.getName ^Class x)
|
#?@(:clj [(class? x) (.getName ^Class x)])
|
||||||
(instance? OptionalKey x) (pr-str (list 'opt (:k x)))
|
(instance? schema.core.OptionalKey x) (pr-str (list 'opt (:k x)))
|
||||||
(instance? RequiredKey x) (pr-str (list 'req (:k x)))
|
(instance? schema.core.RequiredKey x) (pr-str (list 'req (:k x)))
|
||||||
(and (satisfies? s/Schema x) (record? x)) (try (pr-str (s/explain x)) (catch Exception _ x))
|
(and (satisfies? s/Schema x) (record? x)) (try (pr-str (s/explain x)) (catch #?(:clj Exception :cljs js/Error) _ x))
|
||||||
(instance? ValidationError x) (str (su/validation-error-explain x))
|
(instance? schema.utils.ValidationError x) (str (su/validation-error-explain x))
|
||||||
(instance? NamedError x) (str (su/named-error-explain x))
|
(instance? schema.utils.NamedError x) (str (su/named-error-explain x))
|
||||||
:else x))
|
:else x))
|
||||||
schema))
|
schema))
|
||||||
|
|
||||||
|
|
@ -45,12 +43,8 @@
|
||||||
|
|
||||||
(get-apidocs [_ _ {:keys [parameters responses] :as info}]
|
(get-apidocs [_ _ {:keys [parameters responses] :as info}]
|
||||||
(cond-> (dissoc info :parameters :responses)
|
(cond-> (dissoc info :parameters :responses)
|
||||||
parameters (assoc
|
parameters (assoc ::swagger/parameters parameters)
|
||||||
::swagger/parameters
|
responses (assoc ::swagger/responses responses)))
|
||||||
parameters)
|
|
||||||
responses (assoc
|
|
||||||
::swagger/responses
|
|
||||||
responses)))
|
|
||||||
|
|
||||||
(make-open [_ schema] (st/open-schema schema))
|
(make-open [_ schema] (st/open-schema schema))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,9 +51,7 @@
|
||||||
(memoize #(into-spec %1 (gensym "spec"))))
|
(memoize #(into-spec %1 (gensym "spec"))))
|
||||||
|
|
||||||
(defn stringify-pred [pred]
|
(defn stringify-pred [pred]
|
||||||
(str (if (instance? clojure.lang.LazySeq pred)
|
(str (if (seq? pred) (seq pred) pred)))
|
||||||
(seq pred)
|
|
||||||
pred)))
|
|
||||||
|
|
||||||
(defmulti coerce-response? identity :default ::default)
|
(defmulti coerce-response? identity :default ::default)
|
||||||
(defmethod coerce-response? ::default [_] true)
|
(defmethod coerce-response? ::default [_] true)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
(ns reitit.coercion-test
|
(ns reitit.coercion-test
|
||||||
(:require [clojure.test :refer [deftest testing is]]
|
(:require [clojure.test :refer [deftest testing is]]
|
||||||
|
[schema.core :as s]
|
||||||
[reitit.ring :as ring]
|
[reitit.ring :as ring]
|
||||||
[reitit.ring.coercion :as coercion]
|
[reitit.ring.coercion :as coercion]
|
||||||
[reitit.ring.coercion.spec :as spec]
|
[reitit.ring.coercion.spec :as spec]
|
||||||
[schema.core :as s]
|
|
||||||
[reitit.ring.coercion.schema :as schema])
|
[reitit.ring.coercion.schema :as schema])
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(:import (clojure.lang ExceptionInfo))))
|
(:import (clojure.lang ExceptionInfo))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue