4

For Example:

(:require [org.httpkit.client :as http])

(defn post-callback
 []
 ;; how to know if it is due to timeout?
 )

(def options {:body "abc" :timeout 1000})
(http/post "some-url" options post-callback)

If the "some-url" is down, then on timeout, the "post-callback" is called. But within the callback function, how to see if it is invoked due to timeout. Please let me know if there is a way to do so. Thanks.

1 Answers1

3

This is how you can easily reproduce timeout :

(http/get "http://google.com" {:timeout 1}
         (fn [{:keys [status headers body error]}] ;; asynchronous response handling
           (if error
             (do
               (if (instance? org.httpkit.client.TimeoutException error)
                 (println "There was timeout")
                 (println "There wasn't timeout"))
               (println "Failed, exception is " error))
             (println "Async HTTP GET: " status))))

It will print error which is an instance of org.httpkit.client.TimeoutException

So you have to change your callback to accept a map. In case of error the :error field in this map is not nil and in case of timeout it will contain TimeoutException. Btw this is just slightly modified example from the client documentation - I think it is nicely explained in there.

So try changing your callback to :

(defn post-callback
  [{:keys [status headers body error]}]
  ;; and check the error same way as I do above
)
Viktor K.
  • 2,670
  • 2
  • 19
  • 30