mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
Merge pull request #204 from kalekale/spec-coercion-error-docs
Add something to the docs about printing readable spec coercion errors
This commit is contained in:
commit
d1d02b232d
1 changed files with 66 additions and 0 deletions
|
|
@ -58,3 +58,69 @@ Failing coercion:
|
||||||
(match-by-path-and-coerce! "/metosin/users/ikitommi")
|
(match-by-path-and-coerce! "/metosin/users/ikitommi")
|
||||||
; => ExceptionInfo Request coercion failed...
|
; => ExceptionInfo Request coercion failed...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Error printing
|
||||||
|
|
||||||
|
Spec problems are exposed as-is into request & response coercion errors, enabling pretty-printers like expound to be used:
|
||||||
|
|
||||||
|
```clj
|
||||||
|
(require '[reitit.ring :as ring])
|
||||||
|
(require '[reitit.ring.middleware.exception :as exception])
|
||||||
|
(require '[reitit.ring.coercion :as coercion])
|
||||||
|
(require '[expound.alpha :as expound])
|
||||||
|
|
||||||
|
(defn coercion-error-handler [status]
|
||||||
|
(let [printer (expound/custom-printer {:theme :figwheel-theme, :print-specs? false})
|
||||||
|
handler (exception/create-coercion-handler status)]
|
||||||
|
(fn [exception request]
|
||||||
|
(printer (-> exception ex-data :problems))
|
||||||
|
(handler exception request))))
|
||||||
|
|
||||||
|
(def app
|
||||||
|
(ring/ring-handler
|
||||||
|
(ring/router
|
||||||
|
["/plus"
|
||||||
|
{:get
|
||||||
|
{:parameters {:query {:x int?, :y int?}}
|
||||||
|
:responses {200 {:body {:total pos-int?}}}
|
||||||
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
|
{:status 200, :body {:total (+ x y)}})}}]
|
||||||
|
{:data {:coercion reitit.coercion.spec/coercion
|
||||||
|
:middleware [(exception/create-exception-middleware
|
||||||
|
(merge
|
||||||
|
exception/default-handlers
|
||||||
|
{:reitit.coercion/request-coercion (coercion-error-handler 400)
|
||||||
|
:reitit.coercion/response-coercion (coercion-error-handler 500)}))
|
||||||
|
coercion/coerce-request-middleware
|
||||||
|
coercion/coerce-response-middleware]}})))
|
||||||
|
|
||||||
|
(app
|
||||||
|
{:uri "/plus"
|
||||||
|
:request-method :get
|
||||||
|
:query-params {"x" "1", "y" "fail"}})
|
||||||
|
; => ...
|
||||||
|
; -- Spec failed --------------------
|
||||||
|
;
|
||||||
|
; {:x ..., :y "fail"}
|
||||||
|
; ^^^^^^
|
||||||
|
;
|
||||||
|
; should satisfy
|
||||||
|
;
|
||||||
|
; int?
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(app
|
||||||
|
{:uri "/plus"
|
||||||
|
:request-method :get
|
||||||
|
:query-params {"x" "1", "y" "-2"}})
|
||||||
|
; => ...
|
||||||
|
;-- Spec failed --------------------
|
||||||
|
;
|
||||||
|
; {:total -1}
|
||||||
|
; ^^
|
||||||
|
;
|
||||||
|
; should satisfy
|
||||||
|
;
|
||||||
|
; pos-int?
|
||||||
|
```
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue