Revision 88a10df5 htools/Ganeti/BasicTypes.hs

b/htools/Ganeti/BasicTypes.hs
26 26
  , eitherToResult
27 27
  , annotateResult
28 28
  , annotateIOError
29
  , exitIfBad
30 29
  ) where
31 30

  
32 31
import Control.Monad
33
import System.IO (hPutStrLn, stderr)
34
import System.Exit
35 32

  
36 33
-- | This is similar to the JSON library Result type - /very/ similar,
37 34
-- but we want to use it in multiple places, so we abstract it into a
......
81 78
annotateIOError :: String -> IOError -> IO (Result a)
82 79
annotateIOError description exc =
83 80
  return . Bad $ description ++ ": " ++ show exc
84

  
85
-- | Unwraps a 'Result', exiting the program if it is a 'Bad' value,
86
-- otherwise returning the actual contained value.
87
exitIfBad :: Result a -> IO a
88
exitIfBad (Bad s) = do
89
  hPutStrLn stderr $ "Failure: " ++ s
90
  exitWith (ExitFailure 1)
91
exitIfBad (Ok v) = return v

Also available in: Unified diff