Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
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
- data AnnotatedException exception = AnnotatedException {
- annotations :: [Annotation]
- exception :: exception
- exceptionWithCallStack :: (Exception e, HasCallStack) => e -> AnnotatedException e
- throwWithCallStack :: forall e m a. (MonadIO m, Exception e, HasCallStack) => e -> m a
- checkpoint :: forall m a. (MonadUnliftIO m, HasCallStack) => Annotation -> m a -> m a
- checkpointMany :: forall m a. (MonadUnliftIO m, HasCallStack) => [Annotation] -> m a -> m a
- checkpointCallStack :: forall m a. (MonadUnliftIO m, HasCallStack) => m a -> m a
- checkpointCallStackWith :: forall m a. (MonadUnliftIO m, HasCallStack) => [Annotation] -> m a -> m a
- catch :: forall e m a. (MonadUnliftIO m, Exception e, HasCallStack) => m a -> (e -> m a) -> m a
- catches :: forall m a. (MonadUnliftIO m, HasCallStack) => m a -> [Handler m a] -> m a
- tryAnnotated :: forall e m a. (MonadUnliftIO m, Exception e) => m a -> m (Either (AnnotatedException e) a)
- try :: forall e m a. (MonadUnliftIO m, Exception e) => m a -> m (Either e a)
- check :: Exception e => AnnotatedException SomeException -> Maybe (AnnotatedException e)
- hide :: Exception e => AnnotatedException e -> AnnotatedException SomeException
- annotatedExceptionCallStack :: AnnotatedException exception -> Maybe CallStack
- addCallStackToException :: CallStack -> AnnotatedException exception -> AnnotatedException exception
- data Annotation where
- Annotation :: AnnC a => a -> Annotation
- newtype CallStackAnnotation = CallStackAnnotation {
- unCallStackAnnotation :: [(String, SrcLoc)]
- class (Typeable e, Show e) => Exception e where
- toException :: e -> SomeException
- fromException :: SomeException -> Maybe e
- displayException :: e -> String
- data SomeException = Exception e => SomeException e
- throw :: forall e m a. (MonadIO m, Exception e, HasCallStack) => e -> m a
- data Handler (m :: Type -> Type) a = Exception e => Handler (e -> m a)
- class Monad m => MonadIO (m :: Type -> Type) where
- class MonadIO m => MonadUnliftIO (m :: Type -> Type) where
- withRunInIO :: ((forall a. m a -> IO a) -> IO b) -> m b
The Main Type
data AnnotatedException exception Source #
The AnnotatedException
type wraps an exception
with
a [
. This can provide a sort of a manual stack trace with
programmer provided data.Annotation
]
Since: 0.1.0.0
Constructors
AnnotatedException | |
Fields
|
Instances
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
hide :: Exception e => AnnotatedException e -> AnnotatedException SomeException Source #
Call toException
on the underlying Exception
.
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 [
, then this will likely break.Annotation
]
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 show
ing
the Annotation
if it is otherwise unrecognized.
Since: 0.1.0.0
Constructors
Annotation :: AnnC a => a -> Annotation |
Instances
IsString Annotation Source # | Since: 0.1.0.0 |
Defined in Data.Annotation Methods fromString :: String -> Annotation # | |
Show Annotation Source # | Since: 0.1.0.0 |
Defined in Data.Annotation Methods showsPrec :: Int -> Annotation -> ShowS # show :: Annotation -> String # showList :: [Annotation] -> ShowS # |
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 |
Fields
|
Instances
Show CallStackAnnotation Source # | |
Defined in Data.Annotation Methods showsPrec :: Int -> CallStackAnnotation -> ShowS # show :: CallStackAnnotation -> String # showList :: [CallStackAnnotation] -> ShowS # | |
Eq CallStackAnnotation Source # | |
Defined in Data.Annotation Methods (==) :: CallStackAnnotation -> CallStackAnnotation -> Bool # (/=) :: CallStackAnnotation -> CallStackAnnotation -> Bool # |
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
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
Exception SomeException | Since: base-3.0 |
Defined in GHC.Exception.Type Methods toException :: SomeException -> SomeException # fromException :: SomeException -> Maybe SomeException # displayException :: SomeException -> String # | |
Show SomeException | Since: base-3.0 |
Defined in GHC.Exception.Type Methods showsPrec :: Int -> SomeException -> ShowS # show :: SomeException -> String # showList :: [SomeException] -> ShowS # |
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
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
Lift a computation from the IO
monad.
This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations
(i.e. IO
is the base monad for the stack).
Example
import Control.Monad.Trans.State -- from the "transformers" library printState :: Show s => StateT s IO () printState = do state <- get liftIO $ print state
Had we omitted
, we would have ended up with this error:liftIO
• Couldn't match type ‘IO’ with ‘StateT s IO’ Expected type: StateT s IO () Actual type: IO ()
The important part here is the mismatch between StateT s IO ()
and
.IO
()
Luckily, we know of a function that takes an
and returns an IO
a(m a)
:
,
enabling us to run the program and see the expected results:liftIO
> evalStateT printState "hello" "hello" > evalStateT printState 3 3
Instances
MonadIO IO | Since: base-4.9.0.0 |
Defined in Control.Monad.IO.Class | |
MonadIO m => MonadIO (MaybeT m) | |
Defined in Control.Monad.Trans.Maybe | |
MonadIO m => MonadIO (ExceptT e m) | |
Defined in Control.Monad.Trans.Except | |
MonadIO m => MonadIO (IdentityT m) | |
Defined in Control.Monad.Trans.Identity | |
MonadIO m => MonadIO (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
MonadIO m => MonadIO (StateT s m) | |
Defined in Control.Monad.Trans.State.Lazy | |
MonadIO m => MonadIO (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
(Monoid w, MonadIO m) => MonadIO (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.Lazy | |
(Monoid w, MonadIO m) => MonadIO (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.Strict | |
MonadIO m => MonadIO (ContT r m) | |
Defined in Control.Monad.Trans.Cont | |
(Monoid w, MonadIO m) => MonadIO (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.Lazy | |
(Monoid w, MonadIO m) => MonadIO (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.Strict |
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 function run
provided by withRunInIO
, it must meet the
monad transformer laws as reformulated for MonadUnliftIO
:
run . return = return
run (m >>= f) = run m >>= run . f
Instances of MonadUnliftIO
must also satisfy the following laws:
- Identity law
withRunInIO (\run -> run m) = m
- Inverse law
withRunInIO (\_ -> m) = liftIO m
As an example of an invalid instance, a naive implementation of
MonadUnliftIO (StateT s m)
might be
withRunInIO inner = StateT $ \s -> withRunInIO $ \run -> inner (run . flip evalStateT s)
This breaks the identity law because the inner run m
would throw away
any state changes in m
.
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
MonadUnliftIO IO | |
Defined in Control.Monad.IO.Unlift | |
MonadUnliftIO m => MonadUnliftIO (IdentityT m) | |
Defined in Control.Monad.IO.Unlift | |
MonadUnliftIO m => MonadUnliftIO (ReaderT r m) | |
Defined in Control.Monad.IO.Unlift |