{-# LANGUAGE DerivingVia #-}
module Control.Effect.ErrorIO
  ( -- * Effects
    ErrorIO(..)
  , X.Exception(..)
  , SomeException

    -- * Actions
  , throwIO
  , catchIO

    -- * Interpretations
  , errorIOToIO

  , errorIOToError

    -- * MonadCatch
  , C.MonadCatch

    -- * Carriers
  , ErrorIOToIOC
  , ErrorIOToErrorC
  ) where

import Control.Effect
import Control.Effect.Type.ErrorIO
import Control.Effect.Type.Throw
import Control.Effect.Type.Catch

import Control.Effect.Internal.ErrorIO

import Control.Exception (SomeException)
import qualified Control.Exception as X
import qualified Control.Monad.Catch as C

throwIO :: (X.Exception e, Eff ErrorIO m) => e -> m a
throwIO :: e -> m a
throwIO = ErrorIO m a -> m a
forall (e :: Effect) (m :: * -> *) a.
(Member e (Derivs m), Carrier m) =>
e m a -> m a
send (ErrorIO m a -> m a) -> (e -> ErrorIO m a) -> e -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> ErrorIO m a
forall e (m :: * -> *) a. Exception e => e -> ErrorIO m a
ThrowIO

catchIO :: (X.Exception e, Eff ErrorIO m) => m a -> (e -> m a) -> m a
catchIO :: m a -> (e -> m a) -> m a
catchIO m a
m e -> m a
h = ErrorIO m a -> m a
forall (e :: Effect) (m :: * -> *) a.
(Member e (Derivs m), Carrier m) =>
e m a -> m a
send (m a -> (e -> m a) -> ErrorIO m a
forall e (m :: * -> *) a.
Exception e =>
m a -> (e -> m a) -> ErrorIO m a
CatchIO m a
m e -> m a
h)

data ErrorIOToErrorH

instance ( Eff (Error SomeException) m
         , Carrier m
         )
      => Handler ErrorIOToErrorH ErrorIO m where
  effHandler :: ErrorIO (Effly z) x -> Effly z x
effHandler = \case
    ThrowIO e
e -> Throw SomeException (Effly z) x -> Effly z x
forall (e :: Effect) (m :: * -> *) a.
(Member e (Derivs m), Carrier m) =>
e m a -> m a
send (Throw SomeException (Effly z) x -> Effly z x)
-> Throw SomeException (Effly z) x -> Effly z x
forall a b. (a -> b) -> a -> b
$ SomeException -> Throw SomeException (Effly z) x
forall e (m :: * -> *) a. e -> Throw e m a
Throw (e -> SomeException
forall e. Exception e => e -> SomeException
X.toException e
e)
    CatchIO Effly z x
m e -> Effly z x
h -> Catch SomeException (Effly z) x -> Effly z x
forall (e :: Effect) (m :: * -> *) a.
(Member e (Derivs m), Carrier m) =>
e m a -> m a
send (Catch SomeException (Effly z) x -> Effly z x)
-> Catch SomeException (Effly z) x -> Effly z x
forall a b. (a -> b) -> a -> b
$ Effly z x
-> (SomeException -> Effly z x) -> Catch SomeException (Effly z) x
forall (m :: * -> *) a e. m a -> (e -> m a) -> Catch e m a
Catch Effly z x
m ((SomeException -> Effly z x) -> Catch SomeException (Effly z) x)
-> (SomeException -> Effly z x) -> Catch SomeException (Effly z) x
forall a b. (a -> b) -> a -> b
$ \SomeException
e -> case SomeException -> Maybe e
forall e. Exception e => SomeException -> Maybe e
X.fromException SomeException
e of
      Just e
e' -> e -> Effly z x
h e
e'
      Maybe e
_       -> Throw SomeException (Effly z) x -> Effly z x
forall (e :: Effect) (m :: * -> *) a.
(Member e (Derivs m), Carrier m) =>
e m a -> m a
send (Throw SomeException (Effly z) x -> Effly z x)
-> Throw SomeException (Effly z) x -> Effly z x
forall a b. (a -> b) -> a -> b
$ SomeException -> Throw SomeException (Effly z) x
forall e (m :: * -> *) a. e -> Throw e m a
Throw SomeException
e
  {-# INLINEABLE effHandler #-}


type ErrorIOToErrorC = InterpretC ErrorIOToErrorH ErrorIO

-- | Transform an @'ErrorIO'@ effect into an @'Error' 'SomeException'@
-- effect.
errorIOToError :: Eff (Error SomeException) m
               => ErrorIOToErrorC m a
               -> m a
errorIOToError :: ErrorIOToErrorC m a -> m a
errorIOToError = ErrorIOToErrorC m a -> m a
forall h (e :: Effect) (m :: * -> *) a.
Handler h e m =>
InterpretC h e m a -> m a
interpretViaHandler
{-# INLINE errorIOToError #-}