2017-08-16 73 views
2

我有一個使用http-client https://www.stackage.org/haddock/lts-9.0/http-client-0.5.7.0/Network-HTTP-Client.html#v:httpLbs的yesod應用程序。我打電話給如何捕獲由httpLbs(http-client)引發的異常

resp <- httpLbs req man 

裏面的Handler (Response BSL.ByteString) monad。

我得到這個(在應用程序的日誌)

6/Aug/2017:15:14:17 +0200 [Error#yesod-core] HttpExceptionRequest Request { ... 

(的下一行代碼永遠不會被執行)

相反,我想捕獲異常,並在我的代碼處理它。怎麼樣?

回答

4

我建議使用safe-exceptions函數庫中的tryAny函數。粗略地說,這將是這樣的:

eres <- tryAny $ httpLbs req man 
case eres of 
    Left e -> handleException e 
    Right lbs -> handleBody lbs 

使用安全的例外將幫助你避免一些極端情況,否則你可能遇到與異步異常和單子變壓器。