From 9b13c3e3febd0a4db5f1f1e19e0077ac3103d94b Mon Sep 17 00:00:00 2001 From: Paula Gearon Date: Wed, 7 Feb 2018 13:13:38 -0500 Subject: [PATCH 1/2] handling errors in EDN coercion --- src/clj_http/client.clj | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/clj_http/client.clj b/src/clj_http/client.clj index 78c696a9..3a756553 100644 --- a/src/clj_http/client.clj +++ b/src/clj_http/client.clj @@ -345,14 +345,19 @@ (assoc resp :body (String. ^"[B" body charset))))) (defn coerce-clojure-body - [request {:keys [body] :as resp}] + [request {:keys [body status] :as resp}] (let [^String charset (or (-> resp :content-type-params :charset) "UTF-8") - body (util/force-byte-array body)] + body (util/force-byte-array body) + body-str (and (seq body) (String. ^"[B" body charset))] (assoc resp :body (cond (empty? body) nil - edn-enabled? (parse-edn (String. ^"[B" body charset)) + edn-enabled? (if (unexceptional-status? status) + (parse-edn body-str) + (try + (parse-edn body-str) + (catch Exception _ body-str))) :else (binding [*read-eval* false] - (read-string (String. ^"[B" body charset))))))) + (read-string body-str)))))) (defn coerce-transit-body [{:keys [transit-opts coerce] :as request} From f6bd0f9cb17c68ebf8b99d292a4bc15884ec53e9 Mon Sep 17 00:00:00 2001 From: Paula Gearon Date: Wed, 7 Feb 2018 22:17:27 -0500 Subject: [PATCH 2/2] Tests for EDN errors --- test/clj_http/test/client_test.clj | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/clj_http/test/client_test.clj b/test/clj_http/test/client_test.clj index e28f0eeb..db4a29ea 100644 --- a/test/clj_http/test/client_test.clj +++ b/test/clj_http/test/client_test.clj @@ -1148,6 +1148,13 @@ (:body (client/coerce-response-body {:as :x-www-form-urlencoded} www-form-urlencoded-resp)))))) +(deftest t-coercion-method-error + (let [edn-body (ByteArrayInputStream. (.getBytes "error: not found")) + edn-resp {:body edn-body :status 404 + :headers {"content-type" "application/edn"}}] + (is (= "error: not found" + (:body (client/coerce-response-body {:as :clojure} edn-resp)))))) + (deftest ^:integration t-with-middleware (run-server) (is (:request-time (request {:uri "/get" :method :get})))