From 6d7bb6870476a5af46ce33fd22a644af75512679 Mon Sep 17 00:00:00 2001 From: Anna Kuznetsova Date: Fri, 12 Sep 2014 14:50:45 -0700 Subject: [PATCH 1/3] Use custom error handlers in wrap-json functions Currently default-handle-error is used even if a custom handler has been passed in. --- src/ring/middleware/format_params.clj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ring/middleware/format_params.clj b/src/ring/middleware/format_params.clj index 512e562..58005a9 100644 --- a/src/ring/middleware/format_params.clj +++ b/src/ring/middleware/format_params.clj @@ -128,7 +128,7 @@ :predicate predicate :decoder decoder :charset charset - :handle-error default-handle-error)) + :handle-error handle-error)) (defn wrap-json-kw-params "Handles body params in JSON format. Parses map keys as keywords. @@ -142,7 +142,7 @@ :predicate predicate :decoder (fn [struct] (decoder struct true)) :charset charset - :handle-error default-handle-error)) + :handle-error handle-error)) (def ^:no-doc yaml-request? (make-type-request-pred #"^(application|text)/(vnd.+)?(x-)?yaml")) From 9e0bcaa7c496c3e7317a1b7fb86f294ba45dce80 Mon Sep 17 00:00:00 2001 From: Anna Kuznetsova Date: Fri, 12 Sep 2014 15:20:12 -0700 Subject: [PATCH 2/3] Add a unittest for custom error handlers --- test/ring/middleware/format_params_test.clj | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/ring/middleware/format_params_test.clj b/test/ring/middleware/format_params_test.clj index 0b75e49..cabcb6a 100644 --- a/test/ring/middleware/format_params_test.clj +++ b/test/ring/middleware/format_params_test.clj @@ -165,3 +165,16 @@ (fn [request] (is (nil? (:body request))))) {:body nil})) + +(deftest test-custom-handle-error + (letfn [(handle-error [& args] {:status 999})] + (are [format content-type body] + (let [req {:body body + :content-type content-type} + resp ((wrap-restful-params identity + :formats [format] + :handle-error handle-error) + req)] + (= 999 (:status resp))) + :json "application/json" "{:a 1}" + :edn "application/edn" "{\"a\": 1}"))) From 39a9d2279ea422e203b5359c11b050098e8ca90a Mon Sep 17 00:00:00 2001 From: Anna Kuznetsova Date: Fri, 12 Sep 2014 15:26:52 -0700 Subject: [PATCH 3/3] Clean up unit test --- test/ring/middleware/format_params_test.clj | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/ring/middleware/format_params_test.clj b/test/ring/middleware/format_params_test.clj index cabcb6a..959e9f0 100644 --- a/test/ring/middleware/format_params_test.clj +++ b/test/ring/middleware/format_params_test.clj @@ -167,14 +167,13 @@ {:body nil})) (deftest test-custom-handle-error - (letfn [(handle-error [& args] {:status 999})] - (are [format content-type body] - (let [req {:body body - :content-type content-type} - resp ((wrap-restful-params identity - :formats [format] - :handle-error handle-error) - req)] - (= 999 (:status resp))) - :json "application/json" "{:a 1}" - :edn "application/edn" "{\"a\": 1}"))) + (are [format content-type body] + (let [req {:body body + :content-type content-type} + resp ((wrap-restful-params identity + :formats [format] + :handle-error (constantly {:status 999})) + req)] + (= 999 (:status resp))) + :json "application/json" "{:a 1}" + :edn "application/edn" "{\"a\": 1}"))