annotated-exception-0.2.0.4: Exceptions, with checkpoints and context.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Control.Exception.Annotated.UnliftIO

Description

This module presents the same interface as Control.Exception.Annotated, but uses MonadUnliftIO instead of MonadCatch or MonadThrow.

Since: 0.1.2.0

Synopsis

The Main Type

data AnnotatedException exception Source #

The AnnotatedException type wraps an exception with a [Annotation]. This can provide a sort of a manual stack trace with programmer provided data.

Since: 0.1.0.0

Constructors

AnnotatedException 

Fields

Instances

Instances details
Functor AnnotatedException Source # 
Instance details

Defined in Control.Exception.Annotated

Applicative AnnotatedException Source # 
Instance details

Defined in Control.Exception.Annotated

Foldable AnnotatedException Source # 
Instance details

Defined in Control.Exception.Annotated

Methods

fold :: Monoid m => AnnotatedException m -> m #

foldMap :: Monoid m => (a -> m) -> AnnotatedException a -> m #

foldMap' :: Monoid m => (a -> m) -> AnnotatedException a -> m #

foldr :: (a -> b -> b) -> b -> AnnotatedException a -> b #

foldr' :: (a -> b -> b) -> b -> AnnotatedException a -> b #

foldl :: (b -> a -> b) -> b -> AnnotatedException a -> b #

foldl' :: (b -> a -> b) -> b -> AnnotatedException a -> b #

foldr1 :: (a -> a -> a) -> AnnotatedException a -> a #

foldl1 :: (a -> a -> a) -> AnnotatedException a -> a #

toList :: AnnotatedException a -> [a] #

null :: AnnotatedException a -> Bool #

length :: AnnotatedException a -> Int #

elem :: Eq a => a -> AnnotatedException a -> Bool #

maximum :: Ord a => AnnotatedException a -> a #

minimum :: Ord a => AnnotatedException a -> a #

sum :: Num a => AnnotatedException a -> a #

product :: Num a => AnnotatedException a -> a #

Traversable AnnotatedException Source # 
Instance details

Defined in Control.Exception.Annotated

Show exception => Show (AnnotatedException exception) Source # 
Instance details

Defined in Control.Exception.Annotated

Methods

showsPrec :: Int -> AnnotatedException exception -> ShowS #

show :: AnnotatedException exception -> String #

showList :: [AnnotatedException exception] -> ShowS #

Exception exception => Exception (AnnotatedException exception) Source #

This instance of Exception is a bit interesting. It tries to do as much hiding and packing and flattening as possible to ensure that even exception handling machinery outside of this package can still intelligently handle it.

Any Exception can be caught as a AnnotatedException with an empty context, so catching a AnnotatedException e will also catch a regular e and give it an empty set of annotations.

For the most up to date details, see the test suite.

Since: 0.1.0.0

Instance details

Defined in Control.Exception.Annotated

exceptionWithCallStack :: (Exception e, HasCallStack) => e -> AnnotatedException e Source #

Annotate the underlying exception with a CallStack.

Since: 0.2.0.0

throwWithCallStack :: forall e m a. (MonadIO m, Exception e, HasCallStack) => e -> m a Source #

Like throwWithCallStack, but uses MonadIO instead of MonadThrow.

Since: 0.1.2.0

Annotating Exceptions

checkpoint :: forall m a. (MonadUnliftIO m, HasCallStack) => Annotation -> m a -> m a Source #

Like checkpoint, but uses MonadUnliftIO instead of MonadCatch.

Since: 0.1.2.0

checkpointMany :: forall m a. (MonadUnliftIO m, HasCallStack) => [Annotation] -> m a -> m a Source #

Like checkpointMany, but uses MonadUnliftIO instead of MonadCatch.

Since: 0.1.2.0

checkpointCallStack :: forall m a. (MonadUnliftIO m, HasCallStack) => m a -> m a Source #

Like checkpointCallStack, but uses MonadUnliftIO instead of MonadCatch.

Since: 0.2.0.2

checkpointCallStackWith :: forall m a. (MonadUnliftIO m, HasCallStack) => [Annotation] -> m a -> m a Source #

Deprecated: As of annotated-exception-0.2.0.0, this is an alias for checkpointMany

Like checkpointCallStackWith, but uses MonadUnliftIO instead of MonadCatch.

Deprecated in 0.2.0.0 as it is now an alias for checkpointMany.

Since: 0.1.2.0

Handling Exceptions

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

Like catch, but uses MonadUnliftIO instead of MonadCatch.

Since: 0.1.2.0

catches :: forall m a. (MonadUnliftIO m, HasCallStack) => m a -> [Handler m a] -> m a Source #

Like catches, bt uses MonadUnliftIO instead of MonadCatch.

Since: 0.1.2.0

tryAnnotated :: forall e m a. (MonadUnliftIO m, Exception e) => m a -> m (Either (AnnotatedException e) a) Source #

Like tryAnnotated but uses MonadUnliftIO instead of MonadCatch.

Since: 0.1.2.0

try :: forall e m a. (MonadUnliftIO m, Exception e) => m a -> m (Either e a) Source #

Like try but uses MonadUnliftIO instead of MonadCatch.

Since: 0.1.2.0

Manipulating Annotated Exceptions

check :: Exception e => AnnotatedException SomeException -> Maybe (AnnotatedException e) Source #

Call fromException on the underlying Exception, attaching the annotations to the result.

Since: 0.1.0.0

annotatedExceptionCallStack :: AnnotatedException exception -> Maybe CallStack Source #

Retrieves the CallStack from an AnnotatedException if one is present.

The library maintains an internal check that a single CallStack is present in the list, so this only returns the first one found. If you have added a CallStack directly to the [Annotation], then this will likely break.

Since: 0.1.0.0

addCallStackToException :: CallStack -> AnnotatedException exception -> AnnotatedException exception Source #

Adds a CallStack to the given AnnotatedException. This function will search through the existing annotations, and it will not add a second CallStack to the list. Instead, it will append the contents of the given CallStack to the existing one.

This mirrors the behavior of the way HasCallStack actually works.

Since: 0.1.0.0

Re-exports from Data.Annotation

data Annotation where Source #

An Annotation is a wrapper around a value that includes a Typeable constraint so we can later unpack it. It is essentially a Dynamic, but we also include Show so that you can always fall back to simply showing the Annotation if it is otherwise unrecognized.

Since: 0.1.0.0

Constructors

Annotation :: AnnC a => a -> Annotation 

Instances

Instances details
Show Annotation Source #

Since: 0.1.0.0

Instance details

Defined in Data.Annotation

IsString Annotation Source #

Since: 0.1.0.0

Instance details

Defined in Data.Annotation

newtype CallStackAnnotation Source #

Deprecated: You can just use CallStack directly now.

A wrapper type for putting a CallStack into an Annotation. We need this because CallStack does not have an Eq instance.

Deprecated in 0.2.0.0 since you can just put a CallStack directly in an Annotation now that we have no need for an Eq constraint on it.

Since: 0.1.0.0

Constructors

CallStackAnnotation

Deprecated: You can just use CallStack directly now.

Re-exports from Control.Exception.Safe

class (Typeable e, Show e) => Exception e where #

Any type that you wish to throw or catch as an exception must be an instance of the Exception class. The simplest case is a new exception type directly below the root:

data MyException = ThisException | ThatException
    deriving Show

instance Exception MyException

The default method definitions in the Exception class do what we need in this case. You can now throw and catch ThisException and ThatException as exceptions:

*Main> throw ThisException `catch` \e -> putStrLn ("Caught " ++ show (e :: MyException))
Caught ThisException

In more complicated examples, you may wish to define a whole hierarchy of exceptions:

---------------------------------------------------------------------
-- Make the root exception type for all the exceptions in a compiler

data SomeCompilerException = forall e . Exception e => SomeCompilerException e

instance Show SomeCompilerException where
    show (SomeCompilerException e) = show e

instance Exception SomeCompilerException

compilerExceptionToException :: Exception e => e -> SomeException
compilerExceptionToException = toException . SomeCompilerException

compilerExceptionFromException :: Exception e => SomeException -> Maybe e
compilerExceptionFromException x = do
    SomeCompilerException a <- fromException x
    cast a

---------------------------------------------------------------------
-- Make a subhierarchy for exceptions in the frontend of the compiler

data SomeFrontendException = forall e . Exception e => SomeFrontendException e

instance Show SomeFrontendException where
    show (SomeFrontendException e) = show e

instance Exception SomeFrontendException where
    toException = compilerExceptionToException
    fromException = compilerExceptionFromException

frontendExceptionToException :: Exception e => e -> SomeException
frontendExceptionToException = toException . SomeFrontendException

frontendExceptionFromException :: Exception e => SomeException -> Maybe e
frontendExceptionFromException x = do
    SomeFrontendException a <- fromException x
    cast a

---------------------------------------------------------------------
-- Make an exception type for a particular frontend compiler exception

data MismatchedParentheses = MismatchedParentheses
    deriving Show

instance Exception MismatchedParentheses where
    toException   = frontendExceptionToException
    fromException = frontendExceptionFromException

We can now catch a MismatchedParentheses exception as MismatchedParentheses, SomeFrontendException or SomeCompilerException, but not other types, e.g. IOException:

*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: MismatchedParentheses))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeFrontendException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeCompilerException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: IOException))
*** Exception: MismatchedParentheses

Minimal complete definition

Nothing

Methods

toException :: e -> SomeException #

fromException :: SomeException -> Maybe e #

displayException :: e -> String #

Render this exception value in a human-friendly manner.

Default implementation: show.

Since: base-4.8.0.0

Instances

Instances details
Exception BlockedIndefinitelyOnMVar

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception BlockedIndefinitelyOnSTM

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception Deadlock

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception AllocationLimitExceeded

Since: base-4.8.0.0

Instance details

Defined in GHC.IO.Exception

Exception CompactionFailed

Since: base-4.10.0.0

Instance details

Defined in GHC.IO.Exception

Exception AssertionFailed

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception SomeAsyncException

Since: base-4.7.0.0

Instance details

Defined in GHC.IO.Exception

Exception AsyncException

Since: base-4.7.0.0

Instance details

Defined in GHC.IO.Exception

Exception ArrayException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception FixIOException

Since: base-4.11.0.0

Instance details

Defined in GHC.IO.Exception

Exception ExitCode

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception IOException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception ArithException

Since: base-4.0.0.0

Instance details

Defined in GHC.Exception.Type

Exception SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.Type

Exception StringException 
Instance details

Defined in Control.Exception.Safe

Exception SyncExceptionWrapper 
Instance details

Defined in Control.Exception.Safe

Exception AsyncExceptionWrapper 
Instance details

Defined in Control.Exception.Safe

Exception exception => Exception (AnnotatedException exception) Source #

This instance of Exception is a bit interesting. It tries to do as much hiding and packing and flattening as possible to ensure that even exception handling machinery outside of this package can still intelligently handle it.

Any Exception can be caught as a AnnotatedException with an empty context, so catching a AnnotatedException e will also catch a regular e and give it an empty set of annotations.

For the most up to date details, see the test suite.

Since: 0.1.0.0

Instance details

Defined in Control.Exception.Annotated

data SomeException #

The SomeException type is the root of the exception type hierarchy. When an exception of type e is thrown, behind the scenes it is encapsulated in a SomeException.

Constructors

Exception e => SomeException e 

Instances

Instances details
Show SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.Type

Exception SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.Type

throw :: forall e m a. (MonadIO m, Exception e, HasCallStack) => e -> m a Source #

Like throw, but uses MonadIO instead of MonadThrow.

Since: 0.1.2.0

data Handler (m :: Type -> Type) a #

Generalized version of Handler

Constructors

Exception e => Handler (e -> m a) 

Instances

Instances details
Monad m => Functor (Handler m) 
Instance details

Defined in Control.Monad.Catch

Methods

fmap :: (a -> b) -> Handler m a -> Handler m b #

(<$) :: a -> Handler m b -> Handler m a #

class Monad m => MonadIO (m :: Type -> Type) where #

Monads in which IO computations may be embedded. Any monad built by applying a sequence of monad transformers to the IO monad will be an instance of this class.

Instances should satisfy the following laws, which state that liftIO is a transformer of monads:

Methods

liftIO :: IO a -> m a #

Lift a computation from the IO monad.

Instances

Instances details
MonadIO IO

Since: base-4.9.0.0

Instance details

Defined in Control.Monad.IO.Class

Methods

liftIO :: IO a -> IO a #

MonadIO Q 
Instance details

Defined in Language.Haskell.TH.Syntax

Methods

liftIO :: IO a -> Q a #

MonadIO m => MonadIO (IdentityT m) 
Instance details

Defined in Control.Monad.Trans.Identity

Methods

liftIO :: IO a -> IdentityT m a #

(Error e, MonadIO m) => MonadIO (ErrorT e m) 
Instance details

Defined in Control.Monad.Trans.Error

Methods

liftIO :: IO a -> ErrorT e m a #

MonadIO m => MonadIO (ReaderT r m) 
Instance details

Defined in Control.Monad.Trans.Reader

Methods

liftIO :: IO a -> ReaderT r m a #

class MonadIO m => MonadUnliftIO (m :: Type -> Type) where #

Monads which allow their actions to be run in IO.

While MonadIO allows an IO action to be lifted into another monad, this class captures the opposite concept: allowing you to capture the monadic context. Note that, in order to meet the laws given below, the intuition is that a monad must have no monadic state, but may have monadic context. This essentially limits MonadUnliftIO to ReaderT and IdentityT transformers on top of IO.

Laws. For any value u returned by askUnliftIO, it must meet the monad transformer laws as reformulated for MonadUnliftIO:

  • unliftIO u . return = return
  • unliftIO u (m >>= f) = unliftIO u m >>= unliftIO u . f

Instances of MonadUnliftIO must also satisfy the idempotency law:

  • askUnliftIO >>= \u -> (liftIO . unliftIO u) m = m

This law showcases two properties. First, askUnliftIO doesn't change the monadic context, and second, liftIO . unliftIO u is equivalent to id IF called in the same monadic context as askUnliftIO.

Since: unliftio-core-0.1.0.0

Methods

withRunInIO :: ((forall a. m a -> IO a) -> IO b) -> m b #

Convenience function for capturing the monadic context and running an IO action with a runner function. The runner function is used to run a monadic action m in IO.

Since: unliftio-core-0.1.0.0

Instances

Instances details
MonadUnliftIO IO 
Instance details

Defined in Control.Monad.IO.Unlift

Methods

withRunInIO :: ((forall a. IO a -> IO a) -> IO b) -> IO b #

MonadUnliftIO m => MonadUnliftIO (IdentityT m) 
Instance details

Defined in Control.Monad.IO.Unlift

Methods

withRunInIO :: ((forall a. IdentityT m a -> IO a) -> IO b) -> IdentityT m b #

MonadUnliftIO m => MonadUnliftIO (ReaderT r m) 
Instance details

Defined in Control.Monad.IO.Unlift

Methods

withRunInIO :: ((forall a. ReaderT r m a -> IO a) -> IO b) -> ReaderT r m b #