We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents cc045ea + fdf6fb6 commit b5cc7d6Copy full SHA for b5cc7d6
src/Control/Monad/Error/Class.purs
@@ -55,6 +55,14 @@ catchJust p act handler = catchError act handle
55
Nothing -> throwError e
56
Just b -> handler b
57
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
+
66
instance monadThrowEither :: MonadThrow e (Either e) where
67
throwError = Left
68
@@ -81,6 +89,6 @@ withResource
81
89
-> m a
82
90
withResource acquire release kleisli = do
83
91
resource <- acquire
84
- result <- (Right <$> kleisli resource) `catchError` (pure <<< Left)
92
+ result <- try $ kleisli resource
85
93
release resource
86
94
either throwError pure result
0 commit comments