module Network.Protocol.XMPP.ErrorT
( ErrorT (..)
, mapErrorT
) where
import Control.Monad (liftM)
import Control.Monad.Fix (MonadFix, mfix)
import Control.Monad.Trans (MonadIO, liftIO)
import Control.Monad.Trans.Class (MonadTrans, lift)
import qualified Control.Monad.Error as E
import Control.Monad.Error (ErrorType)
import qualified Control.Monad.Reader as R
import Control.Monad.Reader (EnvType)
newtype ErrorT e m a = ErrorT { runErrorT :: m (Either e a) }
instance Functor m => Functor (ErrorT e m) where
fmap f = ErrorT . fmap (fmap f) . runErrorT
instance Monad m => Monad (ErrorT e m) where
return = ErrorT . return . Right
(>>=) m k = ErrorT $ do
x <- runErrorT m
case x of
Left l -> return (Left l)
Right r -> runErrorT (k r)
instance Monad m => E.MonadError (ErrorT e m) where
type ErrorType (ErrorT e m) = e
throwError = ErrorT . return . Left
catchError m h = ErrorT $ do
x <- runErrorT m
case x of
Left l -> runErrorT (h l)
Right r -> return (Right r)
instance MonadTrans (ErrorT e) where
lift = ErrorT . liftM Right
instance R.MonadReader m => R.MonadReader (ErrorT e m) where
type EnvType (ErrorT e m) = EnvType m
ask = lift R.ask
local = mapErrorT . R.local
instance MonadIO m => MonadIO (ErrorT e m) where
liftIO = lift . liftIO
instance MonadFix m => MonadFix (ErrorT e m) where
mfix f = ErrorT $ mfix $ \ex -> runErrorT $ f $ case ex of
Right x -> x
_ -> error "empty mfix parameter"
mapErrorT :: (m (Either e a) -> n (Either e' b))
-> ErrorT e m a
-> ErrorT e' n b
mapErrorT f m = ErrorT (f (runErrorT m))