Skip to content

Commit

Permalink
Merge commit '4774494f4b2a4414379bfdddab8016f0e6605c23' into tentamen…
Browse files Browse the repository at this point in the history
…/master
  • Loading branch information
dakrone committed Nov 10, 2017
2 parents 52498a2 + 4774494 commit fa8d34c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,8 @@ files or =KeyStore= instances.

The client will throw exceptions on, well, exceptional status codes, meaning all
HTTP responses other than =#{200 201 202 203 204 205 206 207 300 301 302 303
307}=. clj-http will throw a [[http://github.com/scgilardi/slingshot][Slingshot]] Stone that can be caught by a regular
=(catch Exception e ...)= or in Slingshot's =try+= block:
307}=. clj-http will throw an ex-info exception that can be caught by a regular
=(catch Exception e ...)= or in [[http://github.com/scgilardi/slingshot][Slingshot]]'s =try+= block:

#+BEGIN_SRC clojure
(client/get "http://example.com/broken")
Expand Down
1 change: 0 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
[org.apache.httpcomponents/httpmime "4.5.3"]
[commons-codec "1.10"]
[commons-io "2.5"]
[slingshot "0.12.2"]
[potemkin "0.4.3"]]
:profiles {:dev {:dependencies [;; optional deps
[cheshire "5.7.1"]
Expand Down
13 changes: 6 additions & 7 deletions src/clj_http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
[clj-http.util :refer [opt] :as util]
[clojure.stacktrace :refer [root-cause]]
[clojure.string :as str]
[clojure.walk :refer [keywordize-keys prewalk]]
[slingshot.slingshot :refer [throw+]])
[clojure.walk :refer [keywordize-keys prewalk]])
(:import (java.io InputStream File ByteArrayOutputStream ByteArrayInputStream)
(java.net URL UnknownHostException)
(org.apache.http.entity BufferedHttpEntity ByteArrayEntity
Expand Down Expand Up @@ -236,11 +235,11 @@
resp
(let [data (assoc resp :type ::unexceptional-status)]
(if (opt req :throw-entire-message)
(throw+ data "clj-http: status %d %s" (:status %) resp)
(throw+ data "clj-http: status %s" (:status %)))))))
(throw (ex-info (format "clj-http: status %d %s" status resp) data))
(throw (ex-info (format "clj-http: status %s" status) data)))))))

(defn wrap-exceptions
"Middleware that throws a slingshot exception if the response is not a
"Middleware that throws a ex-info exception if the response is not a
regular response. If :throw-entire-message? is set to true, the entire
response is used as the message, instead of just the status number."
[client]
Expand Down Expand Up @@ -316,7 +315,7 @@
(respond* resp-r req)
(and max-redirects (> redirects-count max-redirects))
(if (opt req :throw-exceptions)
(throw+ resp-r "Too many redirects: %s" redirects-count)
(throw (ex-info (format "Too many redirects: %s" redirects-count) resp-r))
(respond* resp-r req))
(= 303 status)
(follow-redirect client (assoc req :request-method :get
Expand Down Expand Up @@ -344,7 +343,7 @@
(respond* resp-r req))))

(defn ^:deprecated wrap-redirects
"Middleware that follows redirects in the response. A slingshot exception is
"Middleware that follows redirects in the response. A ex-info exception is
thrown if too many redirects occur. Options
:follow-redirects - default:true, whether to follow redirects
Expand Down
13 changes: 6 additions & 7 deletions test/clj_http/test/client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
[clojure.test :refer :all]
[cognitect.transit :as transit]
[ring.util.codec :refer [form-decode-str]]
[ring.middleware.nested-params :refer [parse-nested-keys]]
[slingshot.slingshot :refer [try+]])
[ring.middleware.nested-params :refer [parse-nested-keys]])
(:import (java.net UnknownHostException)
(java.io ByteArrayInputStream)
(org.apache.http HttpEntity)))
Expand Down Expand Up @@ -510,12 +509,12 @@
(deftest throw-type-field
(let [client (fn [req] {:status 500})
e-client (client/wrap-exceptions client)]
(try+
(try
(e-client {})
(catch [:type :clj-http.client/unexceptional-status] _
(is true))
(catch Object _
(is false ":type selector was not caught.")))))
(catch Exception e
(if (= :clj-http.client/unexceptional-status (:type (ex-data e)))
(is true)
(is false ":type selector was not caught."))))))

(deftest throw-on-exceptional-async
(let [client (fn [req respond raise]
Expand Down

0 comments on commit fa8d34c

Please sign in to comment.