From 0bde5cffff2fbb585c4cae105e6152313492db8b Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Thu, 17 Jan 2019 19:40:32 +0200 Subject: [PATCH] Move pretty printing docs to right place --- doc/coercion/clojure_spec_coercion.md | 66 --------------------------- doc/ring/coercion.md | 66 +++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 66 deletions(-) diff --git a/doc/coercion/clojure_spec_coercion.md b/doc/coercion/clojure_spec_coercion.md index 9a410820..96467530 100644 --- a/doc/coercion/clojure_spec_coercion.md +++ b/doc/coercion/clojure_spec_coercion.md @@ -58,69 +58,3 @@ Failing coercion: (match-by-path-and-coerce! "/metosin/users/ikitommi") ; => 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? -``` diff --git a/doc/ring/coercion.md b/doc/ring/coercion.md index 1d4b0032..e458921e 100644 --- a/doc/ring/coercion.md +++ b/doc/ring/coercion.md @@ -135,6 +135,72 @@ Invalid response: ; :in [:response :body]}} ``` +## Pretty printing spec errors + +Spec problems are exposed as-is into request & response coercion errors, enabling pretty-printers like [expound](https://github.com/bhb/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? +``` + ### Optimizations The coercion middleware are [compiled againts a route](compiling_middleware.md). In the middleware compilation step the actual coercer implementations are constructed for the defined models. Also, the middleware doesn't mount itself if a route doesn't have `:coercion` and `:parameters` or `:responses` defined.