mirror of
https://github.com/metosin/reitit.git
synced 2026-01-07 07:59:50 +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)
|
||||
: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
|
||||
(ex-info
|
||||
(str "Request coercion failed: " (pr-str result))
|
||||
(if serialize-failed-result
|
||||
(str "Request coercion failed: " (pr-str result))
|
||||
"Request coercion failed")
|
||||
(merge
|
||||
(into {} result)
|
||||
{:type ::request-coercion
|
||||
|
|
@ -53,10 +55,12 @@
|
|||
:in [:request in]
|
||||
: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
|
||||
(ex-info
|
||||
(str "Response coercion failed: " (pr-str result))
|
||||
(if serialize-failed-result
|
||||
(str "Response coercion failed: " (pr-str result))
|
||||
"Response coercion failed")
|
||||
(merge
|
||||
(into {} result)
|
||||
{:type ::response-coercion
|
||||
|
|
@ -70,7 +74,7 @@
|
|||
(-> request :muuntaja/request :format))
|
||||
|
||||
;; 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
|
||||
parameter-coercion default-parameter-coercion}}]
|
||||
(if coercion
|
||||
|
|
@ -83,13 +87,13 @@
|
|||
format (extract-request-format request)
|
||||
result (coercer value format)]
|
||||
(if (error? result)
|
||||
(request-coercion-failed! result coercion value in request)
|
||||
(request-coercion-failed! result coercion value in request serialize-failed-result)
|
||||
result))))))))
|
||||
|
||||
(defn extract-response-format-default [request _]
|
||||
(-> 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}}]
|
||||
(if coercion
|
||||
(if-let [coercer (-response-coercer coercion body)]
|
||||
|
|
@ -98,7 +102,7 @@
|
|||
value (:body response)
|
||||
result (coercer value format)]
|
||||
(if (error? result)
|
||||
(response-coercion-failed! result coercion value request response)
|
||||
(response-coercion-failed! result coercion value request response serialize-failed-result)
|
||||
result))))))
|
||||
|
||||
(defn encode-error [data]
|
||||
|
|
|
|||
Loading…
Reference in a new issue