Skip to content

Commit

Permalink
Merge pull request #2 from erikd/master
Browse files Browse the repository at this point in the history
Add module Control.Monad.Trans.Either.Exit
  • Loading branch information
tmcgilchrist committed Jan 14, 2018
2 parents 329b6ef + 8c9a07e commit 9766760
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
37 changes: 37 additions & 0 deletions src/Control/Monad/Trans/Either/Exit.hs
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
8 changes: 5 additions & 3 deletions transformers-either.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: transformers-either
version: 0.0.1
version: 0.0.2
license: BSD3
license-file: LICENSE
author: Tim McGilchrist <timmcgil@gmail.com>
Expand All @@ -14,14 +14,15 @@ description:

Uses a pattern synonym to maintain compatibility with the old EitherT types
but is actually ExceptT under the covers.

source-repository head
type: git
location: https://github.com/tmcgilchrist/transformers-either.git

library
build-depends:
base >= 3 && < 5
, text == 1.2.*
, transformers >= 0.4 && < 0.6

ghc-options:
Expand All @@ -32,3 +33,4 @@ library

exposed-modules:
Control.Monad.Trans.Either
Control.Monad.Trans.Either.Exit

0 comments on commit 9766760

Please sign in to comment.