Skip to content

Commit

Permalink
Add tryError
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysxia committed Mar 4, 2019
1 parent 6eee658 commit 5ae7255
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Control/Monad/Error/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ module Control.Monad.Error.Class (
Error(..),
MonadError(..),
liftEither,
tryError,
) where

import Control.Applicative (Applicative(pure))
import Control.Monad.Trans.Except (Except, ExceptT)
import Control.Monad.Trans.Error (Error(..), ErrorT)
import qualified Control.Monad.Trans.Except as ExceptT (throwE, catchE)
Expand Down Expand Up @@ -118,6 +120,11 @@ where @action1@ returns an 'Either' to represent errors.
liftEither :: MonadError e m => Either e a -> m a
liftEither = either throwError return

-- | Catch an exception and return it in 'Left', or return a successful result
-- in 'Right'.
tryError :: (MonadError e m, Applicative m) => m a -> m (Either e a)
tryError m = catchError (fmap Right m) (pure . Left)

instance MonadError IOException IO where
throwError = ioError
catchError = catch
Expand Down
2 changes: 2 additions & 0 deletions Control/Monad/Except.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ module Control.Monad.Except
-- * Monads with error handling
MonadError(..),
liftEither,
tryError,

-- * The ExceptT monad transformer
ExceptT(ExceptT),
Except,
Expand Down

0 comments on commit 5ae7255

Please sign in to comment.