Skip to content

Commit

Permalink
experimental support for clj-http typed exception that carries respon…
Browse files Browse the repository at this point in the history
…se information
  • Loading branch information
mmcgrana committed Sep 24, 2010
1 parent 585eddd commit f2e93d2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions Readme.md
Expand Up @@ -74,6 +74,7 @@ The client in `clj-http.core` makes HTTP requests according to a given Ring requ
Running the tests:

$ lein deps
$ javac src/clj_http/CljHttpException.java -d classes
$ clj test/server.clj
$ lein test

Expand Down
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject clj-http "0.1.1"
(defproject clj-http "0.1.2-SNAPSHOT"
:description
"A Clojure HTTP library wrapping the Apache HttpComponents client."
:dependencies
Expand Down
10 changes: 10 additions & 0 deletions src/clj_http/CljHttpException.java
@@ -0,0 +1,10 @@
package clj_http;

public class CljHttpException extends Exception {
public final Object data;

public CljHttpException(String msg, Object data) {
super(msg);
this.data = data;
}
}
10 changes: 7 additions & 3 deletions src/clj_http/client.clj
@@ -1,6 +1,7 @@
(ns clj-http.client
"Batteries-included HTTP client."
(:import (java.net URL))
(:import java.net.URL)
(:import clj_http.CljHttpException)
(:require [clojure.contrib.string :as str])
(:require [clj-http.core :as core])
(:require [clj-http.util :as util])
Expand Down Expand Up @@ -29,7 +30,10 @@
(let [{:keys [status] :as resp} (client req)]
(if (unexceptional-status? status)
resp
(throw (Exception. (str status)))))))
(throw (CljHttpException.
(str "Response: " status)
{:type :response
:response resp}))))))


(defn follow-redirect [client req resp]
Expand Down Expand Up @@ -180,10 +184,10 @@
request
(-> #'core/request
wrap-redirects
wrap-exceptions
wrap-decompression
wrap-input-coercion
wrap-output-coercion
wrap-exceptions
wrap-query-params
wrap-basic-auth
wrap-accept
Expand Down
10 changes: 8 additions & 2 deletions test/clj_http/client_test.clj
Expand Up @@ -2,7 +2,8 @@
(:use clojure.test)
(:require [clj-http.client :as client])
(:require [clj-http.util :as util])
(:import (java.util Arrays)))
(:import java.util.Arrays)
(:import clj_http.CljHttpException))

(def base-req
{:scheme "http"
Expand Down Expand Up @@ -65,7 +66,12 @@
(let [client (fn [req] {:status 500})
e-client (client/wrap-exceptions client)]
(is (thrown-with-msg? Exception #"500"
(e-client {})))))
(e-client {})))
(try
(e-client {})
(catch CljHttpException e
(is (= :response (:type (.data e))))
(is (= 500 (:status (:response (.data e)))))))))

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

0 comments on commit f2e93d2

Please sign in to comment.