drone29a / clj-oauth

OAuth Consumer support for Clojure
BSD 2-Clause "Simplified" License
273 stars 52 forks source link

Corresponding exceptions from clj-http. #54

Open func-hs opened 7 years ago

func-hs commented 7 years ago

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. ;)