{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}

module Control.Monad.Except.Catch
    ( ExceptCatchT (..)
    , runExceptCatchT
    , module Control.Monad.Error.Class
    , modifyError
    ) where

import Control.Applicative
import Control.Monad.Catch
import Control.Monad.Error.Class hiding (modifyError)
import qualified Control.Monad.Except as Except
import Control.Monad.IO.Unlift
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Writer

-- | This type is useful for translating a 'MonadError' constraint into
-- 'MonadCatch'. This type does not have an 'Either' return, which means we can
-- provide a 'MonadUnliftIO' instance.
--
-- @since 0.1.0.0
newtype ExceptCatchT e m a = ExceptCatchT { forall e (m :: * -> *) a. ExceptCatchT e m a -> m a
unsafeRunExceptCatchT :: m a }
    deriving newtype
        ( forall a b. a -> ExceptCatchT e m b -> ExceptCatchT e m a
forall a b. (a -> b) -> ExceptCatchT e m a -> ExceptCatchT e m b
forall e (m :: * -> *) a b.
Functor m =>
a -> ExceptCatchT e m b -> ExceptCatchT e m a
forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ExceptCatchT e m a -> ExceptCatchT e m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> ExceptCatchT e m b -> ExceptCatchT e m a
$c<$ :: forall e (m :: * -> *) a b.
Functor m =>
a -> ExceptCatchT e m b -> ExceptCatchT e m a
fmap :: forall a b. (a -> b) -> ExceptCatchT e m a -> ExceptCatchT e m b
$cfmap :: forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ExceptCatchT e m a -> ExceptCatchT e m b
Functor, forall a. a -> ExceptCatchT e m a
forall a b.
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m a
forall a b.
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m b
forall a b.
ExceptCatchT e m (a -> b)
-> ExceptCatchT e m a -> ExceptCatchT e m b
forall a b c.
(a -> b -> c)
-> ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m c
forall {e} {m :: * -> *}.
Applicative m =>
Functor (ExceptCatchT e m)
forall e (m :: * -> *) a. Applicative m => a -> ExceptCatchT e m a
forall e (m :: * -> *) a b.
Applicative m =>
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m a
forall e (m :: * -> *) a b.
Applicative m =>
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m b
forall e (m :: * -> *) a b.
Applicative m =>
ExceptCatchT e m (a -> b)
-> ExceptCatchT e m a -> ExceptCatchT e m b
forall e (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b.
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m a
$c<* :: forall e (m :: * -> *) a b.
Applicative m =>
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m a
*> :: forall a b.
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m b
$c*> :: forall e (m :: * -> *) a b.
Applicative m =>
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m b
liftA2 :: forall a b c.
(a -> b -> c)
-> ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m c
$cliftA2 :: forall e (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m c
<*> :: forall a b.
ExceptCatchT e m (a -> b)
-> ExceptCatchT e m a -> ExceptCatchT e m b
$c<*> :: forall e (m :: * -> *) a b.
Applicative m =>
ExceptCatchT e m (a -> b)
-> ExceptCatchT e m a -> ExceptCatchT e m b
pure :: forall a. a -> ExceptCatchT e m a
$cpure :: forall e (m :: * -> *) a. Applicative m => a -> ExceptCatchT e m a
Applicative, forall a. a -> ExceptCatchT e m a
forall a b.
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m b
forall a b.
ExceptCatchT e m a
-> (a -> ExceptCatchT e m b) -> ExceptCatchT e m b
forall {e} {m :: * -> *}. Monad m => Applicative (ExceptCatchT e m)
forall e (m :: * -> *) a. Monad m => a -> ExceptCatchT e m a
forall e (m :: * -> *) a b.
Monad m =>
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m b
forall e (m :: * -> *) a b.
Monad m =>
ExceptCatchT e m a
-> (a -> ExceptCatchT e m b) -> ExceptCatchT e m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> ExceptCatchT e m a
$creturn :: forall e (m :: * -> *) a. Monad m => a -> ExceptCatchT e m a
>> :: forall a b.
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m b
$c>> :: forall e (m :: * -> *) a b.
Monad m =>
ExceptCatchT e m a -> ExceptCatchT e m b -> ExceptCatchT e m b
>>= :: forall a b.
ExceptCatchT e m a
-> (a -> ExceptCatchT e m b) -> ExceptCatchT e m b
$c>>= :: forall e (m :: * -> *) a b.
Monad m =>
ExceptCatchT e m a
-> (a -> ExceptCatchT e m b) -> ExceptCatchT e m b
Monad, forall a. IO a -> ExceptCatchT e m a
forall {e} {m :: * -> *}. MonadIO m => Monad (ExceptCatchT e m)
forall e (m :: * -> *) a. MonadIO m => IO a -> ExceptCatchT e m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: forall a. IO a -> ExceptCatchT e m a
$cliftIO :: forall e (m :: * -> *) a. MonadIO m => IO a -> ExceptCatchT e m a
MonadIO, forall a. ExceptCatchT e m a
forall a.
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
forall {e} {m :: * -> *}. MonadPlus m => Monad (ExceptCatchT e m)
forall {e} {m :: * -> *}.
MonadPlus m =>
Alternative (ExceptCatchT e m)
forall e (m :: * -> *) a. MonadPlus m => ExceptCatchT e m a
forall e (m :: * -> *) a.
MonadPlus m =>
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
forall (m :: * -> *).
Alternative m
-> Monad m
-> (forall a. m a)
-> (forall a. m a -> m a -> m a)
-> MonadPlus m
mplus :: forall a.
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
$cmplus :: forall e (m :: * -> *) a.
MonadPlus m =>
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
mzero :: forall a. ExceptCatchT e m a
$cmzero :: forall e (m :: * -> *) a. MonadPlus m => ExceptCatchT e m a
MonadPlus, forall a. ExceptCatchT e m a
forall a. ExceptCatchT e m a -> ExceptCatchT e m [a]
forall a.
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
forall {e} {m :: * -> *}.
Alternative m =>
Applicative (ExceptCatchT e m)
forall e (m :: * -> *) a. Alternative m => ExceptCatchT e m a
forall e (m :: * -> *) a.
Alternative m =>
ExceptCatchT e m a -> ExceptCatchT e m [a]
forall e (m :: * -> *) a.
Alternative m =>
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
forall (f :: * -> *).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: forall a. ExceptCatchT e m a -> ExceptCatchT e m [a]
$cmany :: forall e (m :: * -> *) a.
Alternative m =>
ExceptCatchT e m a -> ExceptCatchT e m [a]
some :: forall a. ExceptCatchT e m a -> ExceptCatchT e m [a]
$csome :: forall e (m :: * -> *) a.
Alternative m =>
ExceptCatchT e m a -> ExceptCatchT e m [a]
<|> :: forall a.
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
$c<|> :: forall e (m :: * -> *) a.
Alternative m =>
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
empty :: forall a. ExceptCatchT e m a
$cempty :: forall e (m :: * -> *) a. Alternative m => ExceptCatchT e m a
Alternative
        , NonEmpty (ExceptCatchT e m a) -> ExceptCatchT e m a
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
forall b.
Integral b =>
b -> ExceptCatchT e m a -> ExceptCatchT e m a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall e (m :: * -> *) a.
Semigroup (m a) =>
NonEmpty (ExceptCatchT e m a) -> ExceptCatchT e m a
forall e (m :: * -> *) a.
Semigroup (m a) =>
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
forall e (m :: * -> *) a b.
(Semigroup (m a), Integral b) =>
b -> ExceptCatchT e m a -> ExceptCatchT e m a
stimes :: forall b.
Integral b =>
b -> ExceptCatchT e m a -> ExceptCatchT e m a
$cstimes :: forall e (m :: * -> *) a b.
(Semigroup (m a), Integral b) =>
b -> ExceptCatchT e m a -> ExceptCatchT e m a
sconcat :: NonEmpty (ExceptCatchT e m a) -> ExceptCatchT e m a
$csconcat :: forall e (m :: * -> *) a.
Semigroup (m a) =>
NonEmpty (ExceptCatchT e m a) -> ExceptCatchT e m a
<> :: ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
$c<> :: forall e (m :: * -> *) a.
Semigroup (m a) =>
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
Semigroup, ExceptCatchT e m a
[ExceptCatchT e m a] -> ExceptCatchT e m a
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall {e} {m :: * -> *} {a}.
Monoid (m a) =>
Semigroup (ExceptCatchT e m a)
forall e (m :: * -> *) a. Monoid (m a) => ExceptCatchT e m a
forall e (m :: * -> *) a.
Monoid (m a) =>
[ExceptCatchT e m a] -> ExceptCatchT e m a
forall e (m :: * -> *) a.
Monoid (m a) =>
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
mconcat :: [ExceptCatchT e m a] -> ExceptCatchT e m a
$cmconcat :: forall e (m :: * -> *) a.
Monoid (m a) =>
[ExceptCatchT e m a] -> ExceptCatchT e m a
mappend :: ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
$cmappend :: forall e (m :: * -> *) a.
Monoid (m a) =>
ExceptCatchT e m a -> ExceptCatchT e m a -> ExceptCatchT e m a
mempty :: ExceptCatchT e m a
$cmempty :: forall e (m :: * -> *) a. Monoid (m a) => ExceptCatchT e m a
Monoid, forall b.
((forall a. ExceptCatchT e m a -> IO a) -> IO b)
-> ExceptCatchT e m b
forall {e} {m :: * -> *}.
MonadUnliftIO m =>
MonadIO (ExceptCatchT e m)
forall e (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. ExceptCatchT e m a -> IO a) -> IO b)
-> ExceptCatchT e m b
forall (m :: * -> *).
MonadIO m
-> (forall b. ((forall a. m a -> IO a) -> IO b) -> m b)
-> MonadUnliftIO m
withRunInIO :: forall b.
((forall a. ExceptCatchT e m a -> IO a) -> IO b)
-> ExceptCatchT e m b
$cwithRunInIO :: forall e (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. ExceptCatchT e m a -> IO a) -> IO b)
-> ExceptCatchT e m b
MonadUnliftIO, MonadReader r, MonadState s
        , MonadWriter w , forall a. (a -> ExceptCatchT e m a) -> ExceptCatchT e m a
forall {e} {m :: * -> *}. MonadFix m => Monad (ExceptCatchT e m)
forall e (m :: * -> *) a.
MonadFix m =>
(a -> ExceptCatchT e m a) -> ExceptCatchT e m a
forall (m :: * -> *).
Monad m -> (forall a. (a -> m a) -> m a) -> MonadFix m
mfix :: forall a. (a -> ExceptCatchT e m a) -> ExceptCatchT e m a
$cmfix :: forall e (m :: * -> *) a.
MonadFix m =>
(a -> ExceptCatchT e m a) -> ExceptCatchT e m a
MonadFix, forall a. String -> ExceptCatchT e m a
forall {e} {m :: * -> *}. MonadFail m => Monad (ExceptCatchT e m)
forall e (m :: * -> *) a.
MonadFail m =>
String -> ExceptCatchT e m a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
fail :: forall a. String -> ExceptCatchT e m a
$cfail :: forall e (m :: * -> *) a.
MonadFail m =>
String -> ExceptCatchT e m a
MonadFail, forall e a. Exception e => e -> ExceptCatchT e m a
forall {e} {m :: * -> *}. MonadThrow m => Monad (ExceptCatchT e m)
forall e (m :: * -> *) e a.
(MonadThrow m, Exception e) =>
e -> ExceptCatchT e m a
forall (m :: * -> *).
Monad m -> (forall e a. Exception e => e -> m a) -> MonadThrow m
throwM :: forall e a. Exception e => e -> ExceptCatchT e m a
$cthrowM :: forall e (m :: * -> *) e a.
(MonadThrow m, Exception e) =>
e -> ExceptCatchT e m a
MonadThrow, forall e a.
Exception e =>
ExceptCatchT e m a
-> (e -> ExceptCatchT e m a) -> ExceptCatchT e m a
forall {e} {m :: * -> *}.
MonadCatch m =>
MonadThrow (ExceptCatchT e m)
forall e (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
ExceptCatchT e m a
-> (e -> ExceptCatchT e m a) -> ExceptCatchT e m a
forall (m :: * -> *).
MonadThrow m
-> (forall e a. Exception e => m a -> (e -> m a) -> m a)
-> MonadCatch m
catch :: forall e a.
Exception e =>
ExceptCatchT e m a
-> (e -> ExceptCatchT e m a) -> ExceptCatchT e m a
$ccatch :: forall e (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
ExceptCatchT e m a
-> (e -> ExceptCatchT e m a) -> ExceptCatchT e m a
MonadCatch, forall b.
((forall a. ExceptCatchT e m a -> ExceptCatchT e m a)
 -> ExceptCatchT e m b)
-> ExceptCatchT e m b
forall a b c.
ExceptCatchT e m a
-> (a -> ExitCase b -> ExceptCatchT e m c)
-> (a -> ExceptCatchT e m b)
-> ExceptCatchT e m (b, c)
forall {e} {m :: * -> *}.
MonadMask m =>
MonadCatch (ExceptCatchT e m)
forall e (m :: * -> *) b.
MonadMask m =>
((forall a. ExceptCatchT e m a -> ExceptCatchT e m a)
 -> ExceptCatchT e m b)
-> ExceptCatchT e m b
forall e (m :: * -> *) a b c.
MonadMask m =>
ExceptCatchT e m a
-> (a -> ExitCase b -> ExceptCatchT e m c)
-> (a -> ExceptCatchT e m b)
-> ExceptCatchT e m (b, c)
forall (m :: * -> *).
MonadCatch m
-> (forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> (forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> (forall a b c.
    m a -> (a -> ExitCase b -> m c) -> (a -> m b) -> m (b, c))
-> MonadMask m
generalBracket :: forall a b c.
ExceptCatchT e m a
-> (a -> ExitCase b -> ExceptCatchT e m c)
-> (a -> ExceptCatchT e m b)
-> ExceptCatchT e m (b, c)
$cgeneralBracket :: forall e (m :: * -> *) a b c.
MonadMask m =>
ExceptCatchT e m a
-> (a -> ExitCase b -> ExceptCatchT e m c)
-> (a -> ExceptCatchT e m b)
-> ExceptCatchT e m (b, c)
uninterruptibleMask :: forall b.
((forall a. ExceptCatchT e m a -> ExceptCatchT e m a)
 -> ExceptCatchT e m b)
-> ExceptCatchT e m b
$cuninterruptibleMask :: forall e (m :: * -> *) b.
MonadMask m =>
((forall a. ExceptCatchT e m a -> ExceptCatchT e m a)
 -> ExceptCatchT e m b)
-> ExceptCatchT e m b
mask :: forall b.
((forall a. ExceptCatchT e m a -> ExceptCatchT e m a)
 -> ExceptCatchT e m b)
-> ExceptCatchT e m b
$cmask :: forall e (m :: * -> *) b.
MonadMask m =>
((forall a. ExceptCatchT e m a -> ExceptCatchT e m a)
 -> ExceptCatchT e m b)
-> ExceptCatchT e m b
MonadMask
        )

-- |
--
-- @since 0.1.0.0
instance (MonadCatch m, Exception e) => MonadError e (ExceptCatchT e m) where
    throwError :: forall a. e -> ExceptCatchT e m a
throwError = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM
    catchError :: forall a.
ExceptCatchT e m a
-> (e -> ExceptCatchT e m a) -> ExceptCatchT e m a
catchError = forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
catch

-- | Run an 'ExceptCatchT' action. This will catch any thrown @e@ exceptions,
-- regardless of whether you used 'throwM' or 'throwError'.
--
-- Any exception that is not mentioned in @e@ will be thrown - this does not
-- catch all exceptions!
--
-- @since 0.1.0.0
runExceptCatchT :: (Exception e, MonadCatch m) => ExceptCatchT e m a -> m (Either e a)
runExceptCatchT :: forall e (m :: * -> *) a.
(Exception e, MonadCatch m) =>
ExceptCatchT e m a -> m (Either e a)
runExceptCatchT = forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
try forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. ExceptCatchT e m a -> m a
unsafeRunExceptCatchT

-- | Like 'Except.modifyError', but it selects the 'ExceptCatchT' instance for 'IO'
-- exceptions instead of the 'ExceptT' instance with an 'Either' error.
--
-- @since 0.1.0.0
modifyError
    :: (Exception e, MonadCatch m, MonadError e' m)
    => (e -> e') -> ExceptCatchT e m a -> m a
modifyError :: forall e (m :: * -> *) e' a.
(Exception e, MonadCatch m, MonadError e' m) =>
(e -> e') -> ExceptCatchT e m a -> m a
modifyError e -> e'
f ExceptCatchT e m a
action =
    forall e (m :: * -> *) a.
(Exception e, MonadCatch m) =>
ExceptCatchT e m a -> m (Either e a)
runExceptCatchT ExceptCatchT e m a
action forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> e'
f) forall (f :: * -> *) a. Applicative f => a -> f a
pure