How to properly handle all possible errors when using the HTTP channel?

I have a code that looks like this:

retryOnTimeout :: IO a -> IO a retryOnTimeout action = catch action $ \ResponseTimeout -> do putStrLn "Timed out. Trying again." threadDelay 5000000 action 

The problem is that there are many other HttpException constructors, and I would like to keep trying again no matter which error it is. Now, if I replaced ResponseTimeout with _ , then I will get a compilation error because it cannot throw an exception type.

I really don't want to provide a type signature for the exception handler.

I know this is not so much duplication, but adding a case for _ seems wrong, because it looks like a saying: if the exception is ResponseTimeout, then do x, but if the exception is something else, do the same. Is there a concise way to use the template, but still let the compiler know what type it is?

+4
source share
1 answer

If you don't care about the exceptional value, use _ completely, but you need to use ScopedTypeVariables or the let clause to specify the type you want.

 {-# LANGUAGE ScopedTypeVariables #-} retryOnTimeout :: IO a -> IO a retryOnTimeout action = catch action $ \ (_ :: HttpException) -> do putStrLn "Timed out. Trying again." threadDelay 5000000 action 
+6
source

Source: https://habr.com/ru/post/1496332/


All Articles