module Control.MaybeEitherMonad
where
import Control.Exception
import Data.Maybe (fromMaybe)
maybeFail :: Monad m => Maybe a -> m a
maybeFail = maybe (fail "Nothing") return
eitherFailS :: Monad m => Either String a -> m a
eitherFailS = either fail return
eitherFail :: (Show s, Monad m) => Either s a -> m a
eitherFail = either (fail . show) return
data NothingException = NothingException
deriving (Show)
instance Exception NothingException where
data LeftException = LeftException String
deriving (Show)
instance Exception LeftException where
maybeThrow :: Maybe a -> a
maybeThrow = fromMaybe (throw NothingException)
eitherThrowS :: Either String a -> a
eitherThrowS = either (throw . LeftException) id
eitherThrow :: Exception err => Either err a -> a
eitherThrow = either throw id
eitherThrowWith :: Exception err => (x -> err) -> Either x a -> a
eitherThrowWith f = either (throw . f) id
optionally :: Monad m => (a -> m ()) -> Maybe a -> m ()
optionally = maybe (return ())