@@ -292,6 +292,7 @@ module Streamly.Internal.Data.Stream.StreamD
292 , onException
292 , onException
293 , finally_
293 , finally_
294 , finally
294 , finally
297 -- * Concurrent Application
298 -- * Concurrent Application
@@ -3138,7 +3139,7 @@ gbracket_
3138 => m c -- ^ before
3139 => m c -- ^ before
3139 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
3140 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
3140 -> (c -> m d) -- ^ after, on normal stop
3141 -> (c -> m d) -- ^ after, on normal stop
3141− -> (c -> e -> Stream m b) -- ^ on exception
3142+ -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
3142 -> (c -> Stream m b) -- ^ stream generator
3143 -> (c -> Stream m b) -- ^ stream generator
3143 -> Stream m b
3144 -> Stream m b
3144gbracket_ bef exc aft fexc fnormal =
3145gbracket_ bef exc aft fexc fnormal =
@@ -3159,7 +3160,8 @@ gbracket_ bef exc aft fexc fnormal =
3159 return $ Yield x (GBracketNormal (Stream step1 s) v)
3160 return $ Yield x (GBracketNormal (Stream step1 s) v)
3160 Skip s -> return $ Skip (GBracketNormal (Stream step1 s) v)
3161 Skip s -> return $ Skip (GBracketNormal (Stream step1 s) v)
3161 Stop -> aft v >> return Stop
3162 Stop -> aft v >> return Stop
3162− Left e -> return $ Skip (GBracketException (fexc v e))
3164+ return $ Skip (GBracketException (fexc v e (UnStream step1 st)))
3163 step gst (GBracketException (UnStream step1 st)) = do
3165 step gst (GBracketException (UnStream step1 st)) = do
3164 res <- step1 gst st
3166 res <- step1 gst st
3165 case res of
3167 case res of
@@ -3220,7 +3222,7 @@ gbracket
3220 => m c -- ^ before
3222 => m c -- ^ before
3221 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
3223 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
3222 -> (c -> m d) -- ^ after, on normal stop or GC
3224 -> (c -> m d) -- ^ after, on normal stop or GC
3223− -> (c -> e -> Stream m b) -- ^ on exception
3225+ -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
3224 -> (c -> Stream m b) -- ^ stream generator
3226 -> (c -> Stream m b) -- ^ stream generator
3225 -> Stream m b
3227 -> Stream m b
3226gbracket bef exc aft fexc fnormal =
3228gbracket bef exc aft fexc fnormal =
@@ -3256,7 +3258,7 @@ gbracket bef exc aft fexc fnormal =
3256 return Stop
3258 return Stop
3257 Left e -> do
3259 Left e -> do
3258 clearIORefFinalizer ref
3260 clearIORefFinalizer ref
3259− return $ Skip (GBracketIOException (fexc v e))
3261+ return $ Skip (GBracketIOException (fexc v e (UnStream step1 st)))
3260 step gst (GBracketIOException (UnStream step1 st)) = do
3262 step gst (GBracketIOException (UnStream step1 st)) = do
3261 res <- step1 gst st
3263 res <- step1 gst st
3262 case res of
3264 case res of
@@ -3332,7 +3334,7 @@ after action (Stream step state) = Stream step' Nothing
3332onException :: MonadCatch m => m b -> Stream m a -> Stream m a
3334onException :: MonadCatch m => m b -> Stream m a -> Stream m a
3333onException action str =
3335onException action str =
3334 gbracket_ (return ()) MC.try return
3336 gbracket_ (return ()) MC.try return
3335− (\_ (e :: MC.SomeException) -> nilM (action >> MC.throwM e))
3337+ (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
3336 (\_ -> str)
3338 (\_ -> str)
3338{-# INLINE_NORMAL _onException #-}
3340{-# INLINE_NORMAL _onException #-}
@@ -3361,7 +3363,7 @@ _onException action (Stream step state) = Stream step' state
3361bracket_ :: MonadCatch m => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3363bracket_ :: MonadCatch m => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3362bracket_ bef aft bet =
3364bracket_ bef aft bet =
3363 gbracket_ bef MC.try aft
3365 gbracket_ bef MC.try aft
3364− (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
3366+ (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e)) bet
3366-- | Run the first action before the stream starts and remember its output,
3368-- | Run the first action before the stream starts and remember its output,
3367-- generate a stream using the output, run the second action providing the
3369-- generate a stream using the output, run the second action providing the
@@ -3373,7 +3375,7 @@ bracket :: (MonadAsync m, MonadCatch m)
3373 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3375 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3374bracket bef aft bet =
3376bracket bef aft bet =
3375 gbracket bef MC.try aft
3377 gbracket bef MC.try aft
3376− (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
3378+ (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e)) bet
3378data BracketState s v = BracketInit | BracketRun s v
3380data BracketState s v = BracketInit | BracketRun s v
@@ -3412,13 +3414,23 @@ finally_ action xs = bracket_ (return ()) (\_ -> action) (const xs)
3412finally :: (MonadAsync m, MonadCatch m) => m b -> Stream m a -> Stream m a
3414finally :: (MonadAsync m, MonadCatch m) => m b -> Stream m a -> Stream m a
3413finally action xs = bracket (return ()) (\_ -> action) (const xs)
3415finally action xs = bracket (return ()) (\_ -> action) (const xs)
3417+-- | When evaluating a stream if an exception occurs, stream
3418+-- evaluation aborts and the specified exception handler is run with
3419+-- the exception and the Stream which threw the exception as argument.
3421+{-# INLINE_NORMAL ghandle #-}
3422+ghandle :: (MonadCatch m, Exception e)
3423+ => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
3425+ gbracket_ (return ()) MC.try return (\_ -> f) (\_ -> str)
3415-- | When evaluating a stream if an exception occurs, stream evaluation aborts
3427-- | When evaluating a stream if an exception occurs, stream evaluation aborts
3416-- and the specified exception handler is run with the exception as argument.
3428-- and the specified exception handler is run with the exception as argument.
3417{-# INLINE_NORMAL handle #-}
3429{-# INLINE_NORMAL handle #-}
3418handle :: (MonadCatch m, Exception e)
3430handle :: (MonadCatch m, Exception e)
3419 => (e -> Stream m a) -> Stream m a -> Stream m a
3431 => (e -> Stream m a) -> Stream m a -> Stream m a
3420handle f str =
3432handle f str =
3421− gbracket_ (return ()) MC.try return (\_ e -> f e) (\_ -> str)
3433+ gbracket_ (return ()) MC.try return (\_ e _ -> f e) (\_ -> str)
3423{-# INLINE_NORMAL _handle #-}
3435{-# INLINE_NORMAL _handle #-}
3424_handle :: (MonadCatch m, Exception e)
3436_handle :: (MonadCatch m, Exception e)