module DBus.Util.MonadError
( ErrorM (..)
, ErrorT (..)
, throwErrorM
, throwErrorT
) where
newtype ErrorM e a = ErrorM { runErrorM :: Either e a }
instance Functor (ErrorM e) where
fmap f m = ErrorM $ case runErrorM m of
Left err -> Left err
Right x -> Right $ f x
instance Monad (ErrorM e) where
return = ErrorM . Right
(>>=) m k = case runErrorM m of
Left err -> ErrorM $ Left err
Right x -> k x
throwErrorM :: e -> ErrorM e a
throwErrorM = ErrorM . Left
newtype ErrorT e m a = ErrorT { runErrorT :: m (Either e a) }
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
throwErrorT :: Monad m => e -> ErrorT e m a
throwErrorT = ErrorT . return . Left