Skip to content

Commit

Permalink
constraint consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-martin committed Nov 30, 2023
1 parent 80fd248 commit 5cc736a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
31 changes: 17 additions & 14 deletions library/Freckle/App/Exception/MonadThrow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,30 @@ import qualified Control.Exception.Annotated as Annotated
import qualified Control.Monad.Catch

-- Throws an exception, wrapped in 'AnnotatedException' which includes a call stack
throwM
:: forall e m a. HasCallStack => MonadThrow m => Exception e => e -> m a
throwM :: forall e m a. (Exception e, MonadThrow m, HasCallStack) => e -> m a
throwM = Annotated.throw

throwString :: forall m a. HasCallStack => MonadThrow m => String -> m a
throwString :: forall m a. (MonadThrow m, HasCallStack) => String -> m a
throwString = throwM . userError

fromJustNoteM :: (HasCallStack, MonadThrow m) => String -> Maybe a -> m a
fromJustNoteM
:: forall m a. (MonadThrow m, HasCallStack) => String -> Maybe a -> m a
fromJustNoteM err = maybe (throwString err) pure

impossible :: (HasCallStack, MonadThrow m) => m a
impossible :: forall m a. (MonadThrow m, HasCallStack) => m a
impossible = throwString "Impossible"

catch :: (HasCallStack, Exception e, MonadCatch m) => m a -> (e -> m a) -> m a
catch
:: forall e m a
. (Exception e, MonadCatch m, HasCallStack)
=> m a
-> (e -> m a)
-> m a
catch = withFrozenCallStack Annotated.catch

catchJust
:: forall e b m a
. (HasCallStack, Exception e, MonadCatch m)
. (Exception e, MonadCatch m, HasCallStack)
=> (e -> Maybe b)
-> m a
-> (b -> m a)
Expand All @@ -62,7 +67,7 @@ catchJust test action handler =

catches
:: forall m a
. (HasCallStack, MonadCatch m)
. (MonadCatch m, HasCallStack)
=> m a
-- ^ Action to run
-> [ExceptionHandler m a]
Expand All @@ -77,8 +82,7 @@ catches action handlers =

try
:: forall e m a
. Exception e
=> MonadCatch m
. (Exception e, MonadCatch m, HasCallStack)
=> m a
-- ^ Action to run
-> m (Either e a)
Expand All @@ -88,8 +92,7 @@ try = withFrozenCallStack Annotated.try

tryJust
:: forall e b m a
. Exception e
=> MonadCatch m
. (Exception e, MonadCatch m, HasCallStack)
=> (e -> Maybe b)
-> m a
-- ^ Action to run
Expand All @@ -101,8 +104,8 @@ tryJust test action =
-- | When dealing with a library that does not use 'AnnotatedException',
-- apply this function to augment its exceptions with call stacks.
checkpointCallStack
:: MonadCatch m
=> HasCallStack
:: forall m a
. (MonadCatch m, HasCallStack)
=> m a
-- ^ Action that might throw whatever types of exceptions
-> m a
Expand Down
25 changes: 11 additions & 14 deletions library/Freckle/App/Exception/MonadUnliftIO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,30 @@ import qualified UnliftIO.Exception
import qualified Control.Exception.Annotated.UnliftIO as Annotated

-- Throws an exception, wrapped in 'AnnotatedException' which includes a call stack
throwM :: forall e m a. HasCallStack => MonadIO m => Exception e => e -> m a
throwM :: forall e m a. (Exception e, MonadIO m, HasCallStack) => e -> m a
throwM = Annotated.throw

throwString :: forall m a. HasCallStack => MonadIO m => String -> m a
throwString :: forall m a. (MonadIO m, HasCallStack) => String -> m a
throwString = throwM . userError

fromJustNoteM :: (HasCallStack, MonadIO m) => String -> Maybe a -> m a
fromJustNoteM
:: forall m a. (MonadIO m, HasCallStack) => String -> Maybe a -> m a
fromJustNoteM err = maybe (throwString err) pure

impossible :: (HasCallStack, MonadIO m) => m a
impossible :: forall m a. (MonadIO m, HasCallStack) => m a
impossible = throwString "Impossible"

catch
:: forall e m a
. (MonadUnliftIO m, Exception e, HasCallStack)
. (Exception e, MonadUnliftIO m, HasCallStack)
=> m a
-> (e -> m a)
-> m a
catch = withFrozenCallStack Annotated.catch

catchJust
:: forall e b m a
. (HasCallStack, Exception e, MonadUnliftIO m)
. (Exception e, MonadUnliftIO m, HasCallStack)
=> (e -> Maybe b)
-> m a
-> (b -> m a)
Expand All @@ -67,8 +68,7 @@ catchJust test action handler =

catches
:: forall m a
. MonadUnliftIO m
=> HasCallStack
. (MonadUnliftIO m, HasCallStack)
=> m a
-- ^ Action to run
-> [ExceptionHandler m a]
Expand All @@ -83,8 +83,7 @@ catches action handlers =

try
:: forall e m a
. Exception e
=> MonadUnliftIO m
. (Exception e, MonadUnliftIO m, HasCallStack)
=> m a
-- ^ Action to run
-> m (Either e a)
Expand All @@ -94,8 +93,7 @@ try = withFrozenCallStack Annotated.try

tryJust
:: forall e b m a
. Exception e
=> MonadUnliftIO m
. (Exception e, MonadUnliftIO m, HasCallStack)
=> (e -> Maybe b)
-> m a
-- ^ Action to run
Expand All @@ -108,8 +106,7 @@ tryJust test action =
-- apply this function to augment its exceptions with call stacks.
checkpointCallStack
:: forall m a
. MonadUnliftIO m
=> HasCallStack
. (MonadUnliftIO m, HasCallStack)
=> m a
-- ^ Action that might throw whatever types of exceptions
-> m a
Expand Down

0 comments on commit 5cc736a

Please sign in to comment.