rescue-0.3.0: More understandable exceptions

Safe HaskellNone
LanguageHaskell2010

Control.Monad.Rescue

Contents

Description

Rescue semantics & helpers

Essentially a type-directed version of Catch.

This is the opposite of Raise, which embeds en error. Rescue takes a potential error out of the surrounding context and either handles or exposes it.

Synopsis

Documentation

rescue :: MonadRescueFrom n m => n a -> (ErrorCase n -> m a) -> m a Source #

Handle all exceptions

>>> type MyErrs = '[FooErr, BarErr]
>>> myErrs = Proxy @MyErrs
>>> :{
goesBoom :: Int -> Rescue MyErrs String
goesBoom x =
  if x > 50
    then return (show x)
    else raise FooErr
:}
>>> handler = catchesOpenUnion (\foo -> "Foo: " <> show foo, \bar -> "Bar:" <> show bar)
>>> rescue (goesBoom 42) (pure . handler) :: Rescue MyErrs String
RescueT (Identity (Right "Foo: FooErr"))

handle :: (MonadRaise m, MonadRescueFrom n m, Handles err n m) => n a -> (err -> m a) -> m a Source #

Guaranteed runs

reattempt :: MonadRescue m => Natural -> m a -> m a Source #

retry without asynchoronous exception cleanup. Useful when not dealing with external resources that may be dangerous to close suddenly.

onRaise :: (MonadRescue m, RaisesOnly m errs) => (OpenUnion errs -> m ()) -> m a -> m (Result errs a) Source #

lastly :: (Errors m `Contains` Errors m, MonadRaise m, MonadRescueFrom m m) => m a -> m b -> m a Source #

Run an additional step, and throw away the result. Return the result of the action passed.

Reexports