2015-08-27 34 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 
)