Skip to content

Instantly share code, notes, and snippets.

@maoe
Created November 7, 2014 07:33
Show Gist options
  • Save maoe/b5f86dae5ed31a72163d to your computer and use it in GitHub Desktop.
Save maoe/b5f86dae5ed31a72163d to your computer and use it in GitHub Desktop.
diff --git a/src/Control/Retry.hs b/src/Control/Retry.hs
index 64bf19f..36daf84 100644
--- a/src/Control/Retry.hs
+++ b/src/Control/Retry.hs
@@ -4,6 +4,7 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE ViewPatterns #-}
-----------------------------------------------------------------------------
-- |
@@ -228,23 +229,24 @@ recovering :: forall m a. (MonadIO m, MonadCatch m)
-> m a
recovering (RetryPolicy policy) hs f = go 0
where
-
- -- | Convert a (e -> m Bool) handler into (e -> m a) so it can
- -- be wired into the 'catches' combinator.
- transHandler :: Int -> Handler m Bool -> Handler m a
- transHandler n (Handler h) = Handler $ \ e -> do
- chk <- h e
- case chk of
- True ->
- case policy n of
- Just delay -> do
- liftIO (threadDelay delay)
- go $! n+1
- Nothing -> throwM e
- False -> throwM e
-
- go n = f `catches` map (transHandler n . ($ n)) hs
-
+ go !n = do
+ r <- try f
+ case r of
+ Left e -> tryHandling (e :: SomeException) hs
+ Right x -> return x
+ where
+ tryHandling e [] = throwM e
+ tryHandling e ((($ n) -> Handler h) : handlers)
+ | Just e' <- fromException e = do
+ chk <- h e'
+ if chk
+ then case policy n of
+ Just delay -> do
+ liftIO $ threadDelay delay
+ go $! n+1
+ Nothing -> throwM e'
+ else throwM e'
+ | otherwise = tryHandling e handlers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment