Skip to content

Commit

Permalink
Add the :throw-entire-message? option to include entire response in E…
Browse files Browse the repository at this point in the history
…xception message
  • Loading branch information
dakrone committed Mar 7, 2012
1 parent bd4a3a5 commit 3d1a271
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
32 changes: 17 additions & 15 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,11 @@ The client transparently accepts and decompresses the `gzip` and

;; body as a file
(client/post "http://site.com/resources"
{:body (clojure.java.io/file "/tmp/foo") :body-encoding
"UTF-8"})
{:body (clojure.java.io/file "/tmp/foo") :body-encoding "UTF-8"})

;; :length is NOT optional for passing an InputStream in
(client/post "http://site.com/resources"
{:body (clojure.java.io/input-stream "/tmp/foo")
:length 1000})
{:body (clojure.java.io/input-stream "/tmp/foo") :length 1000})
```

### Output coercion
Expand Down Expand Up @@ -173,17 +171,21 @@ block:

```clojure
(client/get "http://site.com/broken")
=> Stone Object thrown by throw+: {:status 404, :headers {"server" "nginx/1.0.4",
"x-runtime" "12ms",
"content-encoding" "gzip",
"content-type" "text/html; charset=utf-8",
"date" "Mon, 17 Oct 2011 23:15 :36 GMT",
"cache-control" "no-cache",
"status" "404 Not Found",
"transfer-encoding" "chunked",
"connection" "close"},
:body "...body here..."}
clj-http.client/wrap-exceptions/fn--227 (client.clj:37)
=> ExceptionInfo clj-http: status 404 clj-http.client/wrap-exceptions/fn--583 (client.clj:41)
;; Or, if you would like the Exception message to contain the entire response:
(client/get "http://site.com/broken" {:throw-entire-message? true})
=> ExceptionInfo clj-http: status 404 {:status 404,
:headers {"server" "nginx/1.0.4",
"x-runtime" "12ms",
"content-encoding" "gzip",
"content-type" "text/html; charset=utf-8",
"date" "Mon, 17 Oct 2011 23:15 :36 GMT",
"cache-control" "no-cache",
"status" "404 Not Found",
"transfer-encoding" "chunked",
"connection" "close"},
:body "...body here..."}
clj-http.client/wrap-exceptions/fn--584 (client.clj:42

;; You can also ignore exceptions and handle them yourself:
(client/get "http://site.com/broken" {:throw-exceptions false})
Expand Down
4 changes: 4 additions & 0 deletions changelog.org
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
* Work log
Log of merges/issues/work that's gone in so I know what to put in
the changelog for the next release
** 2012-03-06
- bump Cheshire and slingshot deps
- add the :throw-entire-message? option to include resp in
Exception message
** 2012-02-21
- add ability to redirect to relative paths (ngrunwald)
** Release 0.3.2
Expand Down
4 changes: 3 additions & 1 deletion src/clj_http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
(if (or (not (clojure.core/get req :throw-exceptions true))
(unexceptional-status? status))
resp
(throw+ resp "clj-http: status %s" (:status %))))))
(if (:throw-entire-message? req)
(throw+ resp "clj-http: status %d %s" (:status %) resp)
(throw+ resp "clj-http: status %s" (:status %)))))))

(declare wrap-redirects)

Expand Down
6 changes: 5 additions & 1 deletion test/clj_http/test/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@
(let [client (fn [req] {:status 500})
e-client (client/wrap-exceptions client)]
(is (thrown-with-msg? Exception #"500"
(e-client {})))))
(e-client {}))))
(let [client (fn [req] {:status 500 :body "foo"})
e-client (client/wrap-exceptions client)]
(is (thrown-with-msg? Exception #":body"
(e-client {:throw-entire-message? true})))))

(deftest pass-on-non-exceptional
(let [client (fn [req] {:status 200})
Expand Down

0 comments on commit 3d1a271

Please sign in to comment.