Module

Control.Monad.Error.Class

This module defines the MonadError type class and its instances.

#MonadThrow

class MonadThrow :: Type -> (Type -> Type) -> Constraintclass (Monad m) <= MonadThrow e m | m -> e where

The MonadThrow type class represents those monads which support errors via throwError, where throwError e halts, yielding the error e.

An implementation is provided for ExceptT, and for other monad transformers defined in this library.

Laws:

  • Left zero: throwError e >>= f = throwError e

Members

Instances

#MonadError

class MonadError :: Type -> (Type -> Type) -> Constraintclass (MonadThrow e m) <= MonadError e m | m -> e where

The MonadError type class represents those monads which support catching errors.

  • catchError x f calls the error handler f if an error is thrown during the evaluation of x.

An implementation is provided for ExceptT, and for other monad transformers defined in this library.

Laws:

  • Catch: catchError (throwError e) f = f e
  • Pure: catchError (pure a) f = pure a

Members

Instances

#catchJust

catchJust :: forall e m a b. MonadError e m => (e -> Maybe b) -> m a -> (b -> m a) -> m a

This function allows you to provide a predicate for selecting the exceptions that you're interested in, and handle only those exceptons. If the inner computation throws an exception, and the predicate returns Nothing, then the whole computation will still fail with that exception.

#try

try :: forall e m a. MonadError e m => m a -> m (Either e a)

Return Right if the given action succeeds, Left if it throws.

#withResource

withResource :: forall e m r a. MonadError e m => m r -> (r -> m Unit) -> (r -> m a) -> m a

Make sure that a resource is cleaned up in the event of an exception. The release action is called regardless of whether the body action throws or returns.

#liftMaybe

liftMaybe :: forall m e a. MonadThrow e m => e -> Maybe a -> m a

Lift a Maybe value to a MonadThrow monad.

#liftEither

liftEither :: forall m e a. MonadThrow e m => Either e a -> m a

Lift an Either value to a MonadThrow monad.

Modules