2015-08-27 35 views
3

例如:如何识别由于超时而调用http-kit客户端回调?

(: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) 

如果 “一些-URL” 已关闭,然后超时, “后回调” 之称。但在回调函数中,如何查看是否由于超时而被调用。请让我知道是否有办法这样做。谢谢。

回答

2

这是你怎么能轻易复制超时:

(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)))) 

它将打印错误,是org.httpkit.client.TimeoutException

的实例

所以,你必须改变你的回调接受地图。如果发生错误,该映射中的:error字段不为零,如果发生超时,它将包含TimeoutException。顺便说一句,这是从the client documentation只是稍微修改的例子 - 我认为这是很好解释在那里。

所以试着改变你的回调:

(defn post-callback 
    [{:keys [status headers body error]}] 
    ;; and check the error same way as I do above 
)