Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Control/Monad/Error/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The Error monad (also called the Exception monad).
module Control.Monad.Error.Class (
MonadError(..),
liftEither,
liftMaybe,
tryError,
withError,
handleError,
Expand Down Expand Up @@ -74,7 +75,7 @@ import Control.Monad.Trans.Class (lift)
import Control.Exception (IOException, catch, ioError)
import Control.Monad (Monad)
import Data.Monoid (Monoid)
import Prelude (Either (Left, Right), Maybe (Nothing), either, flip, (.), IO, pure, (<$>), (>>=))
import Prelude (Either (Left, Right), Maybe (Nothing), either, maybe, flip, (.), IO, pure, (<$>), (>>=))

{- |
The strategy of combining computations that can throw exceptions
Expand Down Expand Up @@ -122,6 +123,16 @@ where @action1@ returns an 'Either' to represent errors.
liftEither :: MonadError e m => Either e a -> m a
liftEither = either throwError pure

{- |
Lifts a @'Maybe'@ into any @'MonadError' e@, using a supplied @e@ as the error if the 'Maybe' is 'Nothing'.

> do { val <- liftMaybe e =<< action1; action2 }

where @action1@ returns a 'Maybe'.
-}
liftMaybe :: MonadError e m => e -> Maybe a -> m a
liftMaybe e = maybe (throwError e) pure

instance MonadError IOException IO where
throwError = ioError
catchError = catch
Expand Down