Skip to content

Commit cdcd375

Browse files
committed
Error.Class: add convenice lifting functions
Add functions that allow `Either` and `Maybe` to be easily lifted to a MonadThrow monad.
1 parent 60ed3f6 commit cdcd375

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Add `liftMaybeM` and `liftEitherM` to easily lift `Maybe` and `Either` values
11+
to a `MonadThrow` monad.
1012

1113
Bugfixes:
1214

src/Control/Monad/Error/Class.purs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Control.Monad.Error.Class where
44

55
import Prelude
66

7-
import Data.Maybe (Maybe(..))
87
import Data.Either (Either(..), either)
8+
import Data.Maybe (Maybe(..), maybe)
99
import Effect (Effect)
1010
import Effect.Exception as Ex
1111

@@ -102,3 +102,11 @@ withResource acquire release kleisli = do
102102
result <- try $ kleisli resource
103103
release resource
104104
either throwError pure result
105+
106+
-- | Lift a `Maybe` value to a MonadThrow monad.
107+
liftMaybeM :: forall m e a. MonadThrow e m => e -> Maybe a -> m a
108+
liftMaybeM error = maybe (throwError error) pure
109+
110+
-- | Lift an `Either` value to a MonadThrow monad.
111+
liftEitherM :: forall m e a. MonadThrow e m => Either e a -> m a
112+
liftEitherM = either throwError pure

0 commit comments

Comments
 (0)