-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from erikd/master
Add module Control.Monad.Trans.Either.Exit
- Loading branch information
Showing
2 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Control.Monad.Trans.Either.Exit ( | ||
orDie | ||
, orDieWithCode | ||
) where | ||
|
||
import Control.Applicative (pure) | ||
import Control.Monad ((>>=), (>>)) | ||
|
||
import Data.Either (either) | ||
import Data.Function ((.)) | ||
import Data.Int (Int) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
|
||
import System.Exit (ExitCode(..), exitWith) | ||
import System.IO (IO, stderr, hPutStrLn) | ||
|
||
import Control.Monad.Trans.Either | ||
|
||
|
||
-- | orDieWithCode with an exit code of 1 in case of an error | ||
-- | ||
orDie :: (e -> Text) -> EitherT e IO a -> IO a | ||
orDie = orDieWithCode 1 | ||
|
||
-- | An idiom for failing hard on EitherT errors. | ||
-- | ||
-- *This really dies*. There is no other way to say it. | ||
-- | ||
-- The reason it lives with command line parser tooling, is that it is | ||
-- the only valid place to actually exit like this. Be appropriately | ||
-- wary. | ||
-- | ||
orDieWithCode :: Int -> (e -> Text) -> EitherT e IO a -> IO a | ||
orDieWithCode code render e = | ||
runEitherT e >>= | ||
either (\err -> (hPutStrLn stderr . T.unpack . render) err >> exitWith (ExitFailure code)) pure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters