From bb8cd87475c3f2715e06905f154a9b183177b472 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 14 Feb 2015 21:05:22 -0500 Subject: [PATCH] better default checked route error handlers --- src/clj_webtoolbox/routes/checked.clj | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/clj_webtoolbox/routes/checked.clj b/src/clj_webtoolbox/routes/checked.clj index edada7f..9647698 100644 --- a/src/clj_webtoolbox/routes/checked.clj +++ b/src/clj_webtoolbox/routes/checked.clj @@ -1,6 +1,7 @@ (ns clj-webtoolbox.routes.checked "Functions and macros to assist with writing more declarative Compojure route validations." (:require + [clojure.string :as string] [clojure.edn :as edn] [compojure.core :refer [routing]] [ring.util.response :refer [response?]] @@ -23,13 +24,21 @@ (~fail-response result#)) result#))) -(def default-wrap-checks-error-response - (-> (response/content "Handler checks did not all pass.") - (response/status 500))) +(defn default-wrap-checks-error-response + [{:keys [validation-errors] :as request}] + (let [validation-errors-str (string/join "," validation-errors)] + (-> (response/content (str "Handler checks did not all pass." + (if (seq validation-errors) + (str " Errors in: " validation-errors-str)))) + (response/status 500)))) -(def default-check-error-response - (-> (response/content "Route checks did not all pass.") - (response/status 500))) +(defn default-check-error-response + [{:keys [validation-errors] :as request}] + (let [validation-errors-str (string/join "," validation-errors)] + (-> (response/content (str "Route checks did not all pass." + (if (seq validation-errors) + (str " Errors in: " validation-errors-str)))) + (response/status 500)))) (defmacro checked-routes "Wraps a handler (e.g. subset of routes) with one or more filters (aka 'checks' or @@ -44,7 +53,7 @@ [checks :on-fail fail-response & body])} [checks & body] (let [has-fail-response? (= :on-fail (first body)) - fail-response (if has-fail-response? (second body) default-wrap-checks-error-response) + fail-response (if has-fail-response? (second body) `default-wrap-checks-error-response) body (if has-fail-response? (drop 2 body) body)] `(fn [request#] (let [result# (threaded-checks request# ~checks ~fail-response)] @@ -66,7 +75,7 @@ [:on-fail fail-response & body])} [& body] (let [has-fail-response? (= :on-fail (first body)) - fail-response (if has-fail-response? (second body) default-check-error-response) + fail-response (if has-fail-response? (second body) `default-check-error-response) body (if has-fail-response? (drop 2 body) body)] `(fn [request#] (threaded-checks request# ~body ~fail-response))))