Code Monkey home page Code Monkey logo

Comments (8)

grammati avatar grammati commented on July 17, 2024

+1 from me. I need this too.

The problem is that the exception-throwing middleware discards everything except the status code, which it stringifies and sets as the exception's message. I would like to see the whole response object attached to the exception. But of course, this means having an exception class that can carry a non-string payload.

I see at least three ways to do this:

  1. Revive the "exceptions" branch, which seems to have been started and then abandoned by Mark about a year ago.
  2. Add a dependency on slingshot, which seems to be the closest thing to a community-standard exception library.
  3. Wait for Clojure 1.4, which is (I think) going to add some sort of data-carrying exception type.

I would vote for 2 as the quickest, simplest solution.

from clj-http.

dakrone avatar dakrone commented on July 17, 2024

I will probably do this with a dependency on slingshot, the exceptions branch is basically a stripped down slingshot. I'll work on this.

from clj-http.

kevinburke avatar kevinburke commented on July 17, 2024

How feasible would it be to add a debug mode where you could see all the
headers? Like -vvv in curl.

For the record, the problem was I was sending a body of "From=kevin&Body=hello", like this

client/post url {:body body}

When I changed it to sending a map using form-params it worked fine.

On Monday, October 17, 2011, Chris Perkins wrote:

+1 from me. I need this too.

The problem is that the exception-throwing middleware discards everything
except the status code, which it stringifies and sets as the exception's
message. I would like to see the whole response object attached to the
exception. But of course, this means having an exception class that can
carry a non-string payload.

I see at least three ways to do this:

  1. Revive the "exceptions" branch, which seems to have been started and
    then abandoned by Mark about a year ago.
  2. Add a dependency on slingshot, which seems to be the closest thing to a
    community-standard exception library.
  3. Wait for Clojure 1.4, which is (I think) going to add some sort of
    data-carrying exception type.

I would vote for 2 as the quickest, simplest solution.

Reply to this email directly or view it on GitHub:
#12 (comment)

Kevin Burke | Twilio
phone: 925.271.7005 | kev.inburke.com

from clj-http.

dakrone avatar dakrone commented on July 17, 2024

So now (when the slingshot-exception branch gets merged in) clj-http will throw a Stone with the entire response, which can either be caught with a regular (try (get ...) (catch Exception e ...)) or caught with Slingshot's try+ (excuse the bad formatting):

user=> (get "http://github.com/faoeu")

Stone Object thrown by throw+: {:status 404, :headers {"server" "nginx/1.0.4", "x-runtime" "12ms", "content-encoding" "gzip", "strict-transport-security" "max-age=2592000", "x-frame-options" "deny", "content-type" "text/html; charset=utf-8", "date" "Mon, 17 Oct 2011 23:15 :36 GMT", "set-cookie" "_gh_sess=BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D--ed7eadd474fd37850b68bd3c08a5c55ad0c3c833; path=/; expires=Fri, 01 Jan 2021 00:00:00 GMT; secure; HttpOnly", "cache-control" "no-cache", "status" "404 Not Found", "transfer-encoding" "chunked", "connection" "close"}, :body "....very large body here...."} clj-http.client/wrap-exceptions/fn--227 (client.clj:37)

from clj-http.

dakrone avatar dakrone commented on July 17, 2024

In the same branch, I'm also considering adding a (secret!) :debug flag that prints the request to standard out, so people can see exactly what flags are being set on the HttpRequest object:

user=> (get "http://aoeu.com" {:debug true})
Request:
{:query-string nil,
 :user-info nil,
 :uri "",
 :server-port 80,
 :server-name "aoeu.com",
 :scheme "http",
 :headers {"Accept-Encoding" "gzip, deflate"},
 :request-method :get,
 :debug true}
HttpRequest:
{:requestLine #<BasicRequestLine GET http://aoeu.com:80 HTTP/1.1>,
 :protocolVersion #<HttpVersion HTTP/1.1>,
 :params
 #<BasicHttpParams org.apache.http.params.BasicHttpParams@1fb88122>,
 :method "GET",
 :class org.apache.http.client.methods.HttpGet,
 :allHeaders
 [#<BasicHeader Connection: close>,
  #<BasicHeader Accept-Encoding: gzip, deflate>],
 :aborted false,
 :URI #<URI http://aoeu.com:80>} 

from clj-http.

dakrone avatar dakrone commented on July 17, 2024

I've merged this to master. Can you guys take a look and let me know what you think? If this fits your needs I'll cut a point release so it's available on Clojars.

from clj-http.

grammati avatar grammati commented on July 17, 2024

Works great for me. Thanks.

The :debug flag is nice. One other possibility is to use it to print the response as well if an exception is thrown:

(defn wrap-exceptions [client]
  (fn [req]
    (let [{:keys [status] :as resp} (client req)]
      (if (or (not (clojure.core/get req :throw-exceptions true))
              (unexceptional-status? status))
        resp
        (do
          (when (:debug req)
            (println "Response:")
            (clojure.pprint/pprint resp))
          (throw+ resp))))))

But I can take or leave that - I can catch and print the exception anyway now.

from clj-http.

dakrone avatar dakrone commented on July 17, 2024

Awesome, I released a 0.2.2 version, lemme know if you have any more issues with it.

from clj-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.