Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch an exception thrown by httpLbs (http-client)

Tags:

haskell

yesod

I have a yesod application that uses http-client https://www.stackage.org/haddock/lts-9.0/http-client-0.5.7.0/Network-HTTP-Client.html#v:httpLbs . I am calling

resp <- httpLbs req man

inside the Handler (Response BSL.ByteString) monad.

I am getting this (in the application's log)

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

(the next line of code never gets executed)

Instead, I want to catch the exception, and handle it in my code. How?

like image 745
d8d0d65b3f7cf42 Avatar asked Jan 18 '26 10:01

d8d0d65b3f7cf42


1 Answers

I'd recommend using the tryAny function from the safe-exceptions library. Roughly, this would look like:

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

Using safe-exceptions will help you avoid some corner cases you might otherwise run into with async exceptions and monad transformers.

like image 186
Michael Snoyman Avatar answered Jan 21 '26 01:01

Michael Snoyman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!