Skip to content

Commit 7ece9dc

Browse files
authored
Add withException to Effectful.Exception (#311)
1 parent 4c35c13 commit 7ece9dc

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

effectful-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# effectful-core-2.6.0.0 (????-??-??)
22
* Adjust `generalBracket` with `base >= 4.21` to make use of the new exception
33
annotation mechanism.
4+
* Add `withException` to `Effectful.Exception`.
45
* **Breaking changes**:
56
- Change the order of type parameters in `raise` for better usability.
67
- `Effectful.Error.Static.ErrorWrapper` is no longer caught by `catchSync`.

effectful-core/src/Effectful/Exception.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ module Effectful.Exception
5959
, C.ExitCase(..)
6060
, finally
6161
, onException
62+
, withException
6263

6364
-- * Utils
6465

@@ -512,6 +513,27 @@ onException
512513
onException action handler = reallyUnsafeUnliftIO $ \unlift -> do
513514
E.onException (unlift action) (unlift handler)
514515

516+
-- | A variant of 'onException' that gives access to the exception.
517+
--
518+
-- @since 2.6.0.0
519+
withException
520+
:: E.Exception e
521+
=> Eff es a
522+
-> (e -> Eff es b)
523+
-- ^ Computation to run last when an exception or
524+
-- t'Effectful.Error.Static.Error' was thrown.
525+
-> Eff es a
526+
withException action cleanup = do
527+
#if MIN_VERSION_base(4,21,0)
528+
action `catchNoPropagate` \ec@(E.ExceptionWithContext _ e) -> do
529+
_ <- annotateIO (E.WhileHandling (E.toException ec)) (cleanup e)
530+
rethrowIO ec
531+
#else
532+
action `catch` \e -> do
533+
_ <- cleanup e
534+
throwIO e
535+
#endif
536+
515537
----------------------------------------
516538
-- Utils
517539

effectful/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# effectful-core-2.6.0.0 (????-??-??)
22
* Adjust `generalBracket` with `base >= 4.21` to make use of the new exception
33
annotation mechanism.
4+
* Add `withException` to `Effectful.Exception`.
45
* Re-export `ThreadId` from `Effectful.Concurrent` for convenience.
56
* **Breaking changes**:
67
- Change the order of type parameters in `raise` for better usability.

0 commit comments

Comments
 (0)