mirror of
https://github.com/metosin/reitit.git
synced 2026-02-20 01:26:03 +00:00
Add serialize-failed-result coercion option
False by default, if true will serialize the failed coercion result in the error message
This commit is contained in:
parent
bc4443a935
commit
8398c98595
1 changed files with 12 additions and 8 deletions
|
|
@ -41,10 +41,12 @@
|
||||||
:header (->ParameterCoercion :headers :string true true)
|
:header (->ParameterCoercion :headers :string true true)
|
||||||
:path (->ParameterCoercion :path-params :string true true)})
|
:path (->ParameterCoercion :path-params :string true true)})
|
||||||
|
|
||||||
(defn ^:no-doc request-coercion-failed! [result coercion value in request]
|
(defn ^:no-doc request-coercion-failed! [result coercion value in request serialize-failed-result]
|
||||||
(throw
|
(throw
|
||||||
(ex-info
|
(ex-info
|
||||||
(str "Request coercion failed: " (pr-str result))
|
(if serialize-failed-result
|
||||||
|
(str "Request coercion failed: " (pr-str result))
|
||||||
|
"Request coercion failed")
|
||||||
(merge
|
(merge
|
||||||
(into {} result)
|
(into {} result)
|
||||||
{:type ::request-coercion
|
{:type ::request-coercion
|
||||||
|
|
@ -53,10 +55,12 @@
|
||||||
:in [:request in]
|
:in [:request in]
|
||||||
:request request}))))
|
:request request}))))
|
||||||
|
|
||||||
(defn ^:no-doc response-coercion-failed! [result coercion value request response]
|
(defn ^:no-doc response-coercion-failed! [result coercion value request response serialize-failed-result]
|
||||||
(throw
|
(throw
|
||||||
(ex-info
|
(ex-info
|
||||||
(str "Response coercion failed: " (pr-str result))
|
(if serialize-failed-result
|
||||||
|
(str "Response coercion failed: " (pr-str result))
|
||||||
|
"Response coercion failed")
|
||||||
(merge
|
(merge
|
||||||
(into {} result)
|
(into {} result)
|
||||||
{:type ::response-coercion
|
{:type ::response-coercion
|
||||||
|
|
@ -70,7 +74,7 @@
|
||||||
(-> request :muuntaja/request :format))
|
(-> request :muuntaja/request :format))
|
||||||
|
|
||||||
;; TODO: support faster key walking, walk/keywordize-keys is quite slow...
|
;; TODO: support faster key walking, walk/keywordize-keys is quite slow...
|
||||||
(defn request-coercer [coercion type model {::keys [extract-request-format parameter-coercion]
|
(defn request-coercer [coercion type model {::keys [extract-request-format parameter-coercion serialize-failed-result]
|
||||||
:or {extract-request-format extract-request-format-default
|
:or {extract-request-format extract-request-format-default
|
||||||
parameter-coercion default-parameter-coercion}}]
|
parameter-coercion default-parameter-coercion}}]
|
||||||
(if coercion
|
(if coercion
|
||||||
|
|
@ -83,13 +87,13 @@
|
||||||
format (extract-request-format request)
|
format (extract-request-format request)
|
||||||
result (coercer value format)]
|
result (coercer value format)]
|
||||||
(if (error? result)
|
(if (error? result)
|
||||||
(request-coercion-failed! result coercion value in request)
|
(request-coercion-failed! result coercion value in request serialize-failed-result)
|
||||||
result))))))))
|
result))))))))
|
||||||
|
|
||||||
(defn extract-response-format-default [request _]
|
(defn extract-response-format-default [request _]
|
||||||
(-> request :muuntaja/response :format))
|
(-> request :muuntaja/response :format))
|
||||||
|
|
||||||
(defn response-coercer [coercion body {:keys [extract-response-format]
|
(defn response-coercer [coercion body {:keys [extract-response-format serialize-failed-result]
|
||||||
:or {extract-response-format extract-response-format-default}}]
|
:or {extract-response-format extract-response-format-default}}]
|
||||||
(if coercion
|
(if coercion
|
||||||
(if-let [coercer (-response-coercer coercion body)]
|
(if-let [coercer (-response-coercer coercion body)]
|
||||||
|
|
@ -98,7 +102,7 @@
|
||||||
value (:body response)
|
value (:body response)
|
||||||
result (coercer value format)]
|
result (coercer value format)]
|
||||||
(if (error? result)
|
(if (error? result)
|
||||||
(response-coercion-failed! result coercion value request response)
|
(response-coercion-failed! result coercion value request response serialize-failed-result)
|
||||||
result))))))
|
result))))))
|
||||||
|
|
||||||
(defn encode-error [data]
|
(defn encode-error [data]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue