Skip to content

Commit

Permalink
Add withException to Frontrow.App.Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Jan 31, 2024
1 parent 56a1b8b commit 1e86fc8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions library/Freckle/App/Exception/MonadThrow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Freckle.App.Exception.MonadThrow
, catches
, try
, tryJust
, withException
, checkpoint
, checkpointMany
, checkpointCallStack
Expand All @@ -23,6 +24,7 @@ import Freckle.App.Exception.Types

import Control.Applicative (pure)
import Control.Exception.Annotated (checkpoint, checkpointMany)
import Control.Monad (void)
import Control.Monad.Catch (MonadCatch, MonadMask, MonadThrow)
import Data.Either (Either (..))
import Data.Function (($), (.))
Expand Down Expand Up @@ -104,6 +106,17 @@ tryJust test action =
withFrozenCallStack $ Annotated.catch (Right <$> action) $ \e ->
maybe (Control.Monad.Catch.throwM e) (pure . Left) (test e)

withException
:: forall e a m b
. (Exception e, MonadCatch m, HasCallStack)
=> m a
-> (e -> m b)
-> m a
withException action onException =
withFrozenCallStack $ Annotated.catch action $ \e -> do
void $ onException e
Control.Monad.Catch.throwM e

-- | When dealing with a library that does not use 'AnnotatedException',
-- apply this function to augment its exceptions with call stacks.
checkpointCallStack
Expand Down
13 changes: 13 additions & 0 deletions library/Freckle/App/Exception/MonadUnliftIO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Freckle.App.Exception.MonadUnliftIO
, catches
, try
, tryJust
, withException
, checkpoint
, checkpointMany
, checkpointCallStack
Expand All @@ -23,6 +24,7 @@ import Freckle.App.Exception.Types

import Control.Applicative (pure)
import Control.Exception.Annotated.UnliftIO (checkpoint, checkpointMany)
import Control.Monad (void)
import Data.Either (Either (..))
import Data.Function (($), (.))
import Data.Functor (fmap, (<$>))
Expand Down Expand Up @@ -105,6 +107,17 @@ tryJust test action =
withFrozenCallStack $ Annotated.catch (Right <$> action) $ \e ->
maybe (UnliftIO.Exception.throwIO e) (pure . Left) (test e)

withException
:: forall e a m b
. (Exception e, MonadUnliftIO m, HasCallStack)
=> m a
-> (e -> m b)
-> m a
withException action onException =
withFrozenCallStack $ Annotated.catch action $ \e -> do
void $ onException e
UnliftIO.Exception.throwIO e

-- | When dealing with a library that does not use 'AnnotatedException',
-- apply this function to augment its exceptions with call stacks.
checkpointCallStack
Expand Down

0 comments on commit 1e86fc8

Please sign in to comment.