Add logWarningIfBad, a utility function similar to exitIfBad, that logs a
warning and returns a default value instead of just crashing the program if
the unpacked value is Bad.
Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Klaus Aehlig <aehlig@google.com>
, exitErr
, exitWhen
, exitUnless
+ , logWarningIfBad
, rStripSpace
, newUUID
, getCurrentTime
import Ganeti.BasicTypes
import qualified Ganeti.Constants as C
+import Ganeti.Logging
import Ganeti.Runtime
import System.IO
import System.Exit
exitUnless :: Bool -> String -> IO ()
exitUnless cond = exitWhen (not cond)
+-- | Unwraps a 'Result', logging a warning message and then returning a default
+-- value if it is a 'Bad' value, otherwise returning the actual contained value.
+logWarningIfBad :: String -> a -> Result a -> IO a
+logWarningIfBad msg defVal (Bad s) = do
+ logWarning $ msg ++ ": " ++ s
+ return defVal
+logWarningIfBad _ _ (Ok v) = return v
+
-- | Print a warning, but do not exit.
warn :: String -> IO ()
warn = hPutStrLn stderr . (++) "Warning: "