Skip to content

Commit b5cc7d6

Browse files
authored
Merge pull request #95 from rightfold/master
Add try
2 parents cc045ea + fdf6fb6 commit b5cc7d6

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/Control/Monad/Error/Class.purs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ catchJust p act handler = catchError act handle
5555
Nothing -> throwError e
5656
Just b -> handler b
5757

58+
-- | Return `Right` if the given action succeeds, `Left` if it throws.
59+
try
60+
:: forall e m a
61+
. MonadError e m
62+
=> m a
63+
-> m (Either e a)
64+
try a = (Right <$> a) `catchError` (pure <<< Left)
65+
5866
instance monadThrowEither :: MonadThrow e (Either e) where
5967
throwError = Left
6068

@@ -81,6 +89,6 @@ withResource
8189
-> m a
8290
withResource acquire release kleisli = do
8391
resource <- acquire
84-
result <- (Right <$> kleisli resource) `catchError` (pure <<< Left)
92+
result <- try $ kleisli resource
8593
release resource
8694
either throwError pure result

0 commit comments

Comments
 (0)