module Control.Error.Script (
Script,
runScript,
tryMaybe,
tryEither,
tryIO
) where
import Control.Exception
import Control.Monad.Trans.Either
import Control.Error.Util
import Data.EitherR
import System.IO
import System.Exit
type Script = EitherT String IO
runScript :: Script a -> IO a
runScript s = do
e <- runEitherT s
case e of
Left e -> do
hPutStrLn stderr e
exitFailure
Right a -> return a
tryMaybe :: String -> Maybe a -> Script a
tryMaybe str = tryEither . note str
tryEither :: Either String r -> Script r
tryEither = liftEither
tryIO :: IO a -> Script a
tryIO io = EitherT . fmap (fmapL show)
$ (try :: IO a -> IO (Either SomeException a)) io