Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Error
- newError :: Text -> Error
- addContext :: Text -> Error -> Error
- prettyError :: Error -> Text
- unwrapError :: Either Error p -> p
- expectError :: Text -> Either Error p -> p
Documentation
The canonical Error
type.
It can be
- created from a human-readable error message (
newError
) - more semantic context can be added to an existing
Error
(addContext
) - pretty-printed (
prettyError
)
addContext :: Text -> Error -> Error Source #
Add a higher-level context to an Error
.
For example, your code hits a “file not found” I/O exception.
Instead of propagating it unseen, you catch it and annotate it with addContext
,
and describe why you wanted to open the file in the first place:
addContext "Trying to open config file" $ newError "file not found: ./foo"
This way, when a user see the error, they will understand better what happened:
"Trying to open config file: file not found: ./foo"
See prettyError
.
prettyError :: Error -> Text Source #
Pretty print the error.
It will print all context messages, starting with the outermost.
Example:
prettyError $ newError "file not found: ./foo" ==> "file not found: ./foo" prettyError $ addContext "Trying to open config file" $ newError "file not found: ./foo" ==> "Trying to open config file: file not found: ./foo"
unwrapError :: Either Error p -> p Source #
expectError :: Text -> Either Error p -> p Source #
Return the value from a potentially failing computation.
Abort with the error message if it was an error.
The text message is added to the Error
as additional context before aborting.
Panic: if Error
Example:
exceptError "something bad happened" $ Left (newError "oh no!") ==> *** Exception: something bad happened: oh no! exceptError $ Right 42 ==> 42