As handle is flipped catch, handleError
is flipped catchError.
Modulererebase-1.21.2Haskell2010
Control.Monad.Error.Class
- 1 class
- 6 values
- Packagererebase-1.21.2
- Exports7
- LanguageHaskell2010
- LicenceMIT
- SourceClass.hs
Lifts an Either e into any MonadError e.
do { val <- liftEither =<< action1; action2 }where action1 returns an Either to represent errors.
MonadError analogue of the mapExceptT function. The
computation is unwrapped, a function is applied to the Either, and
the result is lifted into the second MonadError instance.
A different MonadError analogue to the withExceptT function.
Modify the value (and possibly the type) of an error in an ExceptT-transformed
monad, while stripping the ExceptT layer.
This is useful for adapting the MonadError constraint of a computation.
For example:
data DatabaseError = ...
performDatabaseQuery :: (MonadError DatabaseError m, ...) => m PersistedValue
data AppError
= MkDatabaseError DatabaseError
| ...
app :: (MonadError AppError m, ...) => m ()Given these types, performDatabaseQuery cannot be used directly inside
app, because the error types don't match. Using modifyError, an equivalent
function with a different error type can be constructed:
performDatabaseQuery' :: (MonadError AppError m, ...) => m PersistedValue
performDatabaseQuery' = modifyError MkDatabaseError performDatabaseQuerySince the error types do match, performDatabaseQuery' _can_ be used in app,
assuming all other constraints carry over.
This works by instantiating the m in the type of performDatabaseQuery to
ExceptT DatabaseError m', which satisfies the MonadError DatabaseError
constraint. Immediately, the ExceptT DatabaseError layer is unwrapped,
producing Either a DatabaseError or a PersistedValue. If it's the former,
the error is wrapped in MkDatabaseError and re-thrown in the inner monad,
otherwise the result value is returned.
MonadError analogue to the try function.
MonadError analogue to the withExceptT function.
Modify the value (but not the type) of an error. The type is
fixed because of the functional dependency m -> e. If you need
to change the type of e use mapError or modifyError.
The strategy of combining computations that can throw exceptions by bypassing bound functions from the point an exception is thrown to the point that it is handled.
Is parameterized over the type of error information and
the monad type constructor.
It is common to use Either String as the monad type constructor
for an error monad in which error descriptions take the form of strings.
In that case and many other common cases the resulting monad is already defined
as an instance of the MonadError class.
You can also define your own error type and/or use a monad type constructor
other than Either or StringEither .
In these cases you will have to explicitly define instances of the MonadError
class.
(If you are using the deprecated IOErrorControl.Monad.Error or
Control.Monad.Trans.Error, you may also have to define an Error instance.)
Methods
throwError :: e -> m aIs used within a monadic computation to begin exception processing.
catchError :: m a -> (e -> m a) -> m aA handler function to handle previous errors and return to normal execution. A common idiom is:
do { action1; action2; action3 } `catchError` handlerwhere the
actionfunctions can call throwError. Note thathandlerand the do-block must have the same return type.
Instances23MonadError, …
MonadError IOException IODefined in mtl-2.3.1 · Control.Monad.Error.ClassMonadError () MaybeDefined in mtl-2.3.1 · Control.Monad.Error.ClassMonadError e (Either e)Defined in mtl-2.3.1 · Control.Monad.Error.ClassMonadError e m => MonadError e (Free m)Defined in free-5.2 · Control.Monad.FreeMonadError e m => MonadError e (Free m)Defined in free-5.2 · Control.Monad.Free.ApMonadError e m => MonadError e (IterT m)Defined in free-5.2 · Control.Monad.Trans.IterMonadError e m => MonadError e (MaybeT m)Defined in mtl-2.3.1 · Control.Monad.Error.ClassMonad m => MonadError e (ExceptT e m)Defined in mtl-2.3.1 · Control.Monad.Error.ClassMonadError e m => MonadError e (IdentityT m)Defined in mtl-2.3.1 · Control.Monad.Error.ClassMonadError e m => MonadError e (ReaderT r m)Defined in mtl-2.3.1 · Control.Monad.Error.ClassMonadError e m => MonadError e (StateT s m)Defined in mtl-2.3.1 · Control.Monad.Error.ClassMonadError e m => MonadError e (StateT s m)Defined in mtl-2.3.1 · Control.Monad.Error.Class(Applicative f, MonadError e m) => MonadError e (FreeT f m)Defined in free-5.2 · Control.Monad.Trans.Free.Ap(Functor f, MonadError e m) => MonadError e (FreeT f m)Defined in free-5.2 · Control.Monad.Trans.Free(Functor f, MonadError e m) => MonadError e (FT f m)Defined in free-5.2 · Control.Monad.Trans.Free.Church(Monoid w, MonadError e m) => MonadError e (AccumT w m)Defined in mtl-2.3.1 · Control.Monad.Error.Class(Monoid w, MonadError e m) => MonadError e (WriterT w m)Defined in mtl-2.3.1 · Control.Monad.Error.Class(Monoid w, MonadError e m) => MonadError e (WriterT w m)Defined in mtl-2.3.1 · Control.Monad.Error.Class(Monoid w, MonadError e m) => MonadError e (WriterT w m)Defined in mtl-2.3.1 · Control.Monad.Error.Class(Comonad w, MonadError e m) => MonadError e (CoT w m)Defined in kan-extensions-5.2.7 · Control.Monad.Co(Monoid w, MonadError e m) => MonadError e (RWST r w s m)Defined in mtl-2.3.1 · Control.Monad.Error.Class(Monoid w, MonadError e m) => MonadError e (RWST r w s m)Defined in mtl-2.3.1 · Control.Monad.Error.Class(Monoid w, MonadError e m) => MonadError e (RWST r w s m)Defined in mtl-2.3.1 · Control.Monad.Error.Class