Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corresponding exceptions from clj-http. #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Corresponding exceptions from clj-http. #54

wants to merge 3 commits into from

Conversation

func-hs
Copy link

@func-hs func-hs commented Nov 28, 2016

Hi, @mattrepl .
First, Thank you for this useful library for OAuth with Clojure-life. :)
I'm using this to connect between my web application to Twitter APIs.
But I cared following points:

Corresponding the response body for clj-http

The HTTP request functions of clj-http will throw ExceptionInfo via slingshot.

(defn- exceptions-response
  [req {:keys [status] :as resp}]
    (if (unexceptional-status? status)
      resp
      (if (false? (opt req :throw-exceptions))
        resp
        (if (opt req :throw-entire-message)
          (throw+ resp "clj-http: status %d %s" (:status %) resp)
          (throw+ resp "clj-http: status %s" (:status %))))))

throw+ is just it.
Note that clj-http will throw this exception when status code is just 4xx or 5xx.
But current clj-oauth has thrown Exception even if status code is over 300...
The status code 3xx is redirect. It is not error.

(defn- check-success-response [m]
  (let [code (:status m)]
    (if (or (< code 200)
                (>= code 300))
      (throw (new Exception (str "Got non-success code: " code ". "
                                                        "Content: " (:body m))))
      m)))

This is not so good because errors from clj-http has been replaced other errors.
I thought better We follow errors from clj-http because it is doing well currently.
Therefore this code will not be needed.

Not ignoring other fields

(defn post-request-body-decoded [url & [req]]
  (form-decode
   (:body (check-success-response
    (httpclient/post url req)))))

Why did you ignore other fields? Those should not be ignored because are also needed for HTTP connections.
If we need to decode response body, this should be written following code instead:

(defn post-request-body-decoded
  [url & [req]]
  (let [res (httpclient/post url req)]
    (update res
            :body
            (form-decode (:body res)))))

Finally, I'll repeat that clj-oauth is very useful library. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants