{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ImplicitParams #-}
module UnliftIO.Exception
(
throwIO
, throwString
, StringException (..)
, stringException
, throwTo
, impureThrow
, fromEither
, fromEitherIO
, fromEitherM
, mapExceptionM
, catch
, catchIO
, catchAny
, catchDeep
, catchAnyDeep
, catchJust
, handle
, handleIO
, handleAny
, handleDeep
, handleAnyDeep
, handleJust
, try
, tryIO
, tryAny
, tryDeep
, tryAnyDeep
, tryJust
, pureTry
, pureTryDeep
, ESafe.Handler (..)
, catches
, catchesDeep
, catchSyncOrAsync
, handleSyncOrAsync
, trySyncOrAsync
, onException
, bracket
, bracket_
, finally
, withException
, bracketOnError
, bracketOnError_
, ESafe.SyncExceptionWrapper(..)
, toSyncException
, ESafe.AsyncExceptionWrapper(..)
, toAsyncException
, fromExceptionUnwrap
, isSyncException
, isAsyncException
, mask
, uninterruptibleMask
, mask_
, uninterruptibleMask_
, evaluate
, evaluateDeep
, Exception (..)
, Typeable
, SomeException (..)
, SomeAsyncException (..)
, IOException
, EUnsafe.assert
, EUnsafe.asyncExceptionToException
, EUnsafe.asyncExceptionFromException
#if !MIN_VERSION_base(4,8,0)
, displayException
#endif
) where
import Control.Concurrent (ThreadId)
import Control.Monad (liftM)
import Control.Monad.IO.Unlift
import Control.Exception (Exception (..), SomeException (..), IOException, SomeAsyncException (..))
import qualified Control.Exception as EUnsafe
import Control.DeepSeq (NFData (..), ($!!))
import Data.Typeable (Typeable, cast)
import System.IO.Unsafe (unsafePerformIO)
import qualified Control.Exception.Safe as ESafe
import Control.Exception.Safe (Handler(..))
#if MIN_VERSION_base(4,9,0)
import GHC.Stack (prettySrcLoc)
import GHC.Stack.Types (HasCallStack, CallStack, getCallStack)
#endif
catch
:: (MonadUnliftIO m, Exception e)
=> m a
-> (e -> m a)
-> m a
catch :: forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch m a
f e -> m a
g = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. m a -> IO a
run m a
f forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`EUnsafe.catch` \e
e ->
if forall e. Exception e => e -> Bool
isSyncException e
e
then forall a. m a -> IO a
run (e -> m a
g e
e)
else forall e a. Exception e => e -> IO a
EUnsafe.throwIO e
e
catchIO :: MonadUnliftIO m => m a -> (IOException -> m a) -> m a
catchIO :: forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> (IOException -> m a) -> m a
catchIO = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch
catchAny :: MonadUnliftIO m => m a -> (SomeException -> m a) -> m a
catchAny :: forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> (SomeException -> m a) -> m a
catchAny = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch
catchDeep :: (MonadUnliftIO m, Exception e, NFData a)
=> m a -> (e -> m a) -> m a
catchDeep :: forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e, NFData a) =>
m a -> (e -> m a) -> m a
catchDeep m a
m = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch (m a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. (MonadIO m, NFData a) => a -> m a
evaluateDeep)
catchAnyDeep :: (NFData a, MonadUnliftIO m) => m a -> (SomeException -> m a) -> m a
catchAnyDeep :: forall a (m :: * -> *).
(NFData a, MonadUnliftIO m) =>
m a -> (SomeException -> m a) -> m a
catchAnyDeep = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e, NFData a) =>
m a -> (e -> m a) -> m a
catchDeep
catchJust :: (MonadUnliftIO m, Exception e) => (e -> Maybe b) -> m a -> (b -> m a) -> m a
catchJust :: forall (m :: * -> *) e b a.
(MonadUnliftIO m, Exception e) =>
(e -> Maybe b) -> m a -> (b -> m a) -> m a
catchJust e -> Maybe b
f m a
a b -> m a
b = m a
a forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` \e
e -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO e
e)) b -> m a
b forall a b. (a -> b) -> a -> b
$ e -> Maybe b
f e
e
catchSyncOrAsync :: (MonadUnliftIO m, Exception e) => m a -> (e -> m a) -> m a
catchSyncOrAsync :: forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catchSyncOrAsync m a
f e -> m a
g = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. m a -> IO a
run m a
f forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`EUnsafe.catch` \e
e -> forall a. m a -> IO a
run (e -> m a
g e
e)
handle :: (MonadUnliftIO m, Exception e) => (e -> m a) -> m a -> m a
handle :: forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
(e -> m a) -> m a -> m a
handle = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch
handleIO :: MonadUnliftIO m => (IOException -> m a) -> m a -> m a
handleIO :: forall (m :: * -> *) a.
MonadUnliftIO m =>
(IOException -> m a) -> m a -> m a
handleIO = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
(e -> m a) -> m a -> m a
handle
handleAny :: MonadUnliftIO m => (SomeException -> m a) -> m a -> m a
handleAny :: forall (m :: * -> *) a.
MonadUnliftIO m =>
(SomeException -> m a) -> m a -> m a
handleAny = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
(e -> m a) -> m a -> m a
handle
handleDeep :: (MonadUnliftIO m, Exception e, NFData a) => (e -> m a) -> m a -> m a
handleDeep :: forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e, NFData a) =>
(e -> m a) -> m a -> m a
handleDeep = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e, NFData a) =>
m a -> (e -> m a) -> m a
catchDeep
handleAnyDeep :: (MonadUnliftIO m, NFData a) => (SomeException -> m a) -> m a -> m a
handleAnyDeep :: forall (m :: * -> *) a.
(MonadUnliftIO m, NFData a) =>
(SomeException -> m a) -> m a -> m a
handleAnyDeep = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a (m :: * -> *).
(NFData a, MonadUnliftIO m) =>
m a -> (SomeException -> m a) -> m a
catchAnyDeep
handleJust :: (MonadUnliftIO m, Exception e) => (e -> Maybe b) -> (b -> m a) -> m a -> m a
handleJust :: forall (m :: * -> *) e b a.
(MonadUnliftIO m, Exception e) =>
(e -> Maybe b) -> (b -> m a) -> m a -> m a
handleJust e -> Maybe b
f = forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall (m :: * -> *) e b a.
(MonadUnliftIO m, Exception e) =>
(e -> Maybe b) -> m a -> (b -> m a) -> m a
catchJust e -> Maybe b
f)
handleSyncOrAsync :: (MonadUnliftIO m, Exception e) => (e -> m a) -> m a -> m a
handleSyncOrAsync :: forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
(e -> m a) -> m a -> m a
handleSyncOrAsync = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catchSyncOrAsync
try :: (MonadUnliftIO m, Exception e) => m a -> m (Either e a)
try :: forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
try m a
f = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. b -> Either a b
Right m a
f) (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left)
tryIO :: MonadUnliftIO m => m a -> m (Either IOException a)
tryIO :: forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> m (Either IOException a)
tryIO = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
try
tryAny :: MonadUnliftIO m => m a -> m (Either SomeException a)
tryAny :: forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> m (Either SomeException a)
tryAny = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
try
tryDeep :: (MonadUnliftIO m, Exception e, NFData a) => m a -> m (Either e a)
tryDeep :: forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e, NFData a) =>
m a -> m (Either e a)
tryDeep m a
f = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. b -> Either a b
Right (m a
f forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. (MonadIO m, NFData a) => a -> m a
evaluateDeep)) (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left)
tryAnyDeep :: (MonadUnliftIO m, NFData a) => m a -> m (Either SomeException a)
tryAnyDeep :: forall (m :: * -> *) a.
(MonadUnliftIO m, NFData a) =>
m a -> m (Either SomeException a)
tryAnyDeep = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e, NFData a) =>
m a -> m (Either e a)
tryDeep
tryJust :: (MonadUnliftIO m, Exception e) => (e -> Maybe b) -> m a -> m (Either b a)
tryJust :: forall (m :: * -> *) e b a.
(MonadUnliftIO m, Exception e) =>
(e -> Maybe b) -> m a -> m (Either b a)
tryJust e -> Maybe b
f m a
a = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch (forall a b. b -> Either a b
Right forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` m a
a) (\e
e -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO e
e) (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left) (e -> Maybe b
f e
e))
trySyncOrAsync :: (MonadUnliftIO m, Exception e) => m a -> m (Either e a)
trySyncOrAsync :: forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
trySyncOrAsync m a
f = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catchSyncOrAsync (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. b -> Either a b
Right m a
f) (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left)
pureTry :: a -> Either SomeException a
pureTry :: forall a. a -> Either SomeException a
pureTry a
a = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ (forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$! a
a) forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> (SomeException -> m a) -> m a
`catchAny` (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left)
pureTryDeep :: NFData a => a -> Either SomeException a
pureTryDeep :: forall a. NFData a => a -> Either SomeException a
pureTryDeep = forall a. IO a -> a
unsafePerformIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(MonadUnliftIO m, NFData a) =>
m a -> m (Either SomeException a)
tryAnyDeep forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return
catchesHandler :: MonadIO m => [Handler m a] -> SomeException -> m a
catchesHandler :: forall (m :: * -> *) a.
MonadIO m =>
[Handler m a] -> SomeException -> m a
catchesHandler [Handler m a]
handlers SomeException
e = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall {m :: * -> *} {a}. Handler m a -> m a -> m a
tryHandler (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall e a. Exception e => e -> IO a
EUnsafe.throwIO SomeException
e)) [Handler m a]
handlers
where tryHandler :: Handler m a -> m a -> m a
tryHandler (ESafe.Handler e -> m a
handler) m a
res
= case forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e of
Just e
e' -> e -> m a
handler e
e'
Maybe e
Nothing -> m a
res
catches :: MonadUnliftIO m => m a -> [Handler m a] -> m a
catches :: forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> [Handler m a] -> m a
catches m a
io [Handler m a]
handlers = m a
io forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` forall (m :: * -> *) a.
MonadIO m =>
[Handler m a] -> SomeException -> m a
catchesHandler [Handler m a]
handlers
catchesDeep :: (MonadUnliftIO m, NFData a) => m a -> [Handler m a] -> m a
catchesDeep :: forall (m :: * -> *) a.
(MonadUnliftIO m, NFData a) =>
m a -> [Handler m a] -> m a
catchesDeep m a
io [Handler m a]
handlers = (m a
io forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. (MonadIO m, NFData a) => a -> m a
evaluateDeep) forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` forall (m :: * -> *) a.
MonadIO m =>
[Handler m a] -> SomeException -> m a
catchesHandler [Handler m a]
handlers
evaluate :: MonadIO m => a -> m a
evaluate :: forall (m :: * -> *) a. MonadIO m => a -> m a
evaluate = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> IO a
EUnsafe.evaluate
evaluateDeep :: (MonadIO m, NFData a) => a -> m a
evaluateDeep :: forall (m :: * -> *) a. (MonadIO m, NFData a) => a -> m a
evaluateDeep = (forall (m :: * -> *) a. MonadIO m => a -> m a
evaluate forall a b. NFData a => (a -> b) -> a -> b
$!!)
bracket :: MonadUnliftIO m => m a -> (a -> m b) -> (a -> m c) -> m c
bracket :: forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket m a
before a -> m b
after a -> m c
thing = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
EUnsafe.mask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
a
x <- forall a. m a -> IO a
run m a
before
Either SomeException c
res1 <- forall e a. Exception e => IO a -> IO (Either e a)
EUnsafe.try forall a b. (a -> b) -> a -> b
$ forall a. IO a -> IO a
restore forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ a -> m c
thing a
x
case Either SomeException c
res1 of
Left (SomeException
e1 :: SomeException) -> do
Either SomeException b
_ :: Either SomeException b <-
forall e a. Exception e => IO a -> IO (Either e a)
EUnsafe.try forall a b. (a -> b) -> a -> b
$ forall a. IO a -> IO a
EUnsafe.uninterruptibleMask_ forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ a -> m b
after a
x
forall e a. Exception e => e -> IO a
EUnsafe.throwIO SomeException
e1
Right c
y -> do
b
_ <- forall a. IO a -> IO a
EUnsafe.uninterruptibleMask_ forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ a -> m b
after a
x
forall (m :: * -> *) a. Monad m => a -> m a
return c
y
bracket_ :: MonadUnliftIO m => m a -> m b -> m c -> m c
bracket_ :: forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> m b -> m c -> m c
bracket_ m a
before m b
after m c
thing = forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket m a
before (forall a b. a -> b -> a
const m b
after) (forall a b. a -> b -> a
const m c
thing)
bracketOnError :: MonadUnliftIO m => m a -> (a -> m b) -> (a -> m c) -> m c
bracketOnError :: forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracketOnError m a
before a -> m b
after a -> m c
thing = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
EUnsafe.mask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
a
x <- forall a. m a -> IO a
run m a
before
Either SomeException c
res1 <- forall e a. Exception e => IO a -> IO (Either e a)
EUnsafe.try forall a b. (a -> b) -> a -> b
$ forall a. IO a -> IO a
restore forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ a -> m c
thing a
x
case Either SomeException c
res1 of
Left (SomeException
e1 :: SomeException) -> do
Either SomeException b
_ :: Either SomeException b <-
forall e a. Exception e => IO a -> IO (Either e a)
EUnsafe.try forall a b. (a -> b) -> a -> b
$ forall a. IO a -> IO a
EUnsafe.uninterruptibleMask_ forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ a -> m b
after a
x
forall e a. Exception e => e -> IO a
EUnsafe.throwIO SomeException
e1
Right c
y -> forall (m :: * -> *) a. Monad m => a -> m a
return c
y
bracketOnError_ :: MonadUnliftIO m => m a -> m b -> m c -> m c
bracketOnError_ :: forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> m b -> m c -> m c
bracketOnError_ m a
before m b
after m c
thing = forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracketOnError m a
before (forall a b. a -> b -> a
const m b
after) (forall a b. a -> b -> a
const m c
thing)
finally
:: MonadUnliftIO m
=> m a
-> m b
-> m a
finally :: forall (m :: * -> *) a b. MonadUnliftIO m => m a -> m b -> m a
finally m a
thing m b
after = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
EUnsafe.uninterruptibleMask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
Either SomeException a
res1 <- forall e a. Exception e => IO a -> IO (Either e a)
EUnsafe.try forall a b. (a -> b) -> a -> b
$ forall a. IO a -> IO a
restore forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run m a
thing
case Either SomeException a
res1 of
Left (SomeException
e1 :: SomeException) -> do
Either SomeException b
_ :: Either SomeException b <- forall e a. Exception e => IO a -> IO (Either e a)
EUnsafe.try forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run m b
after
forall e a. Exception e => e -> IO a
EUnsafe.throwIO SomeException
e1
Right a
x -> do
b
_ <- forall a. m a -> IO a
run m b
after
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
withException :: (MonadUnliftIO m, Exception e)
=> m a -> (e -> m b) -> m a
withException :: forall (m :: * -> *) e a b.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m b) -> m a
withException m a
thing e -> m b
after = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
EUnsafe.uninterruptibleMask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
Either e a
res1 <- forall e a. Exception e => IO a -> IO (Either e a)
EUnsafe.try forall a b. (a -> b) -> a -> b
$ forall a. IO a -> IO a
restore forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run m a
thing
case Either e a
res1 of
Left e
e1 -> do
Either SomeException b
_ :: Either SomeException b <- forall e a. Exception e => IO a -> IO (Either e a)
EUnsafe.try forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ e -> m b
after e
e1
forall e a. Exception e => e -> IO a
EUnsafe.throwIO e
e1
Right a
x -> forall (m :: * -> *) a. Monad m => a -> m a
return a
x
onException :: MonadUnliftIO m => m a -> m b -> m a
onException :: forall (m :: * -> *) a b. MonadUnliftIO m => m a -> m b -> m a
onException m a
thing m b
after = forall (m :: * -> *) e a b.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m b) -> m a
withException m a
thing (\(SomeException
_ :: SomeException) -> m b
after)
throwIO :: (MonadIO m, Exception e) => e -> m a
throwIO :: forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Exception e => e -> IO a
EUnsafe.throwIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Exception e => e -> SomeException
toSyncException
toSyncException :: Exception e => e -> SomeException
toSyncException :: forall e. Exception e => e -> SomeException
toSyncException =
forall e. Exception e => e -> SomeException
ESafe.toSyncException
toAsyncException :: Exception e => e -> SomeException
toAsyncException :: forall e. Exception e => e -> SomeException
toAsyncException =
forall e. Exception e => e -> SomeException
ESafe.toAsyncException
fromExceptionUnwrap :: Exception e => SomeException -> Maybe e
fromExceptionUnwrap :: forall e. Exception e => SomeException -> Maybe e
fromExceptionUnwrap SomeException
se
| Just (ESafe.AsyncExceptionWrapper e
e) <- forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se = forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast e
e
| Just (ESafe.SyncExceptionWrapper e
e) <- forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se = forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast e
e
| Bool
otherwise = forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se
isSyncException :: Exception e => e -> Bool
isSyncException :: forall e. Exception e => e -> Bool
isSyncException =
forall e. Exception e => e -> Bool
ESafe.isSyncException
isAsyncException :: Exception e => e -> Bool
isAsyncException :: forall e. Exception e => e -> Bool
isAsyncException = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Exception e => e -> Bool
isSyncException
{-# INLINE isAsyncException #-}
#if !MIN_VERSION_base(4,8,0)
displayException :: Exception e => e -> String
displayException = show
#endif
mask :: MonadUnliftIO m => ((forall a. m a -> m a) -> m b) -> m b
mask :: forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> m a) -> m b) -> m b
mask (forall a. m a -> m a) -> m b
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
EUnsafe.mask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask ->
forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ (forall a. m a -> m a) -> m b
f forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> IO a
unmask forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. m a -> IO a
run
uninterruptibleMask :: MonadUnliftIO m => ((forall a. m a -> m a) -> m b) -> m b
uninterruptibleMask :: forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> m a) -> m b) -> m b
uninterruptibleMask (forall a. m a -> m a) -> m b
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
EUnsafe.uninterruptibleMask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask ->
forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ (forall a. m a -> m a) -> m b
f forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> IO a
unmask forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. m a -> IO a
run
mask_ :: MonadUnliftIO m => m a -> m a
mask_ :: forall (m :: * -> *) a. MonadUnliftIO m => m a -> m a
mask_ m a
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. IO a -> IO a
EUnsafe.mask_ (forall a. m a -> IO a
run m a
f)
uninterruptibleMask_ :: MonadUnliftIO m => m a -> m a
uninterruptibleMask_ :: forall (m :: * -> *) a. MonadUnliftIO m => m a -> m a
uninterruptibleMask_ m a
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. IO a -> IO a
EUnsafe.uninterruptibleMask_ (forall a. m a -> IO a
run m a
f)
#if MIN_VERSION_base(4,9,0)
throwString :: (MonadIO m, HasCallStack) => String -> m a
throwString :: forall (m :: * -> *) a. (MonadIO m, HasCallStack) => String -> m a
throwString String
s = forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (String -> CallStack -> StringException
StringException String
s HasCallStack
?callStack)
#else
throwString :: MonadIO m => String -> m a
throwString s = throwIO (StringException s ())
#endif
#if MIN_VERSION_base(4,9,0)
stringException :: HasCallStack => String -> StringException
stringException :: HasCallStack => String -> StringException
stringException String
s = String -> CallStack -> StringException
StringException String
s HasCallStack
?callStack
#else
stringException :: String -> StringException
stringException s = StringException s ()
#endif
#if MIN_VERSION_base(4,9,0)
data StringException = StringException String CallStack
deriving Typeable
instance Show StringException where
show :: StringException -> String
show (StringException String
s CallStack
cs) = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
forall a b. (a -> b) -> a -> b
$ String
"UnliftIO.Exception.throwString called with:\n\n"
forall a. a -> [a] -> [a]
: String
s
forall a. a -> [a] -> [a]
: String
"\nCalled from:\n"
forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map (String, SrcLoc) -> String
go (CallStack -> [(String, SrcLoc)]
getCallStack CallStack
cs)
where
go :: (String, SrcLoc) -> String
go (String
x, SrcLoc
y) = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ String
" "
, String
x
, String
" ("
, SrcLoc -> String
prettySrcLoc SrcLoc
y
, String
")\n"
]
#else
data StringException = StringException String ()
deriving Typeable
instance Show StringException where
show (StringException s _) = "UnliftIO.Exception.throwString called with:\n\n" ++ s
#endif
instance Eq StringException where
StringException String
msg1 CallStack
_ == :: StringException -> StringException -> Bool
== StringException String
msg2 CallStack
_ = String
msg1 forall a. Eq a => a -> a -> Bool
== String
msg2
instance Exception StringException
throwTo :: (Exception e, MonadIO m) => ThreadId -> e -> m ()
throwTo :: forall e (m :: * -> *).
(Exception e, MonadIO m) =>
ThreadId -> e -> m ()
throwTo ThreadId
tid = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Exception e => ThreadId -> e -> IO ()
EUnsafe.throwTo ThreadId
tid forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Exception e => e -> SomeException
toAsyncException
impureThrow :: Exception e => e -> a
impureThrow :: forall e a. Exception e => e -> a
impureThrow = forall a e. Exception e => e -> a
EUnsafe.throw forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Exception e => e -> SomeException
toSyncException
fromEither :: (Exception e, MonadIO m) => Either e a -> m a
fromEither :: forall e (m :: * -> *) a.
(Exception e, MonadIO m) =>
Either e a -> m a
fromEither = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO forall (m :: * -> *) a. Monad m => a -> m a
return
fromEitherIO :: (Exception e, MonadIO m) => IO (Either e a) -> m a
fromEitherIO :: forall e (m :: * -> *) a.
(Exception e, MonadIO m) =>
IO (Either e a) -> m a
fromEitherIO = forall e (m :: * -> *) a.
(Exception e, MonadIO m) =>
m (Either e a) -> m a
fromEitherM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
fromEitherM :: (Exception e, MonadIO m) => m (Either e a) -> m a
fromEitherM :: forall e (m :: * -> *) a.
(Exception e, MonadIO m) =>
m (Either e a) -> m a
fromEitherM = (forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall e (m :: * -> *) a.
(Exception e, MonadIO m) =>
Either e a -> m a
fromEither)
mapExceptionM :: (Exception e1, Exception e2, MonadUnliftIO m) => (e1 -> e2) -> m a -> m a
mapExceptionM :: forall e1 e2 (m :: * -> *) a.
(Exception e1, Exception e2, MonadUnliftIO m) =>
(e1 -> e2) -> m a -> m a
mapExceptionM e1 -> e2
f = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
(e -> m a) -> m a -> m a
handle (forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. e1 -> e2
f)