module Control.Error.Script (
Script,
runScript,
scriptIO
) where
import Control.Exception (try, SomeException)
import Control.Monad (liftM)
import Control.Monad.IO.Class (MonadIO(liftIO))
import Control.Monad.Trans.Either (EitherT(EitherT, runEitherT))
import Control.Error.Util (errLn)
import Data.EitherR (fmapL)
import System.Exit (exitFailure)
import Control.Monad.Trans.Class (lift)
import System.IO (stderr)
type Script = EitherT String IO
runScript :: Script a -> IO a
runScript s = do
e <- runEitherT s
case e of
Left e -> do
errLn e
exitFailure
Right a -> return a
scriptIO :: (MonadIO m) => IO a -> EitherT String m a
scriptIO = EitherT
. liftIO
. liftM (fmapL show)
. (try :: IO a -> IO (Either SomeException a))