| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Data.Error
Synopsis
- data Error
- newError :: Text -> Error
- showToError :: Show a => a -> Error
- exceptionToError :: Exception exc => exc -> Error
- addContext :: Text -> Error -> Error
- prettyError :: Error -> Text
- expectError :: HasCallStack => Text -> Either Error p -> p
- unwrapError :: HasCallStack => 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)
Error creation
From Show and Exception
These two functions can be helpful, but consider that they don’t always provide very user-friendly error messages.
It is recommended that you use addContext to improve the messages generated by showToError and exceptionToError.
showToError :: Show a => a -> Error Source #
Create an error from a Show type.
If your type implements Exception, it is usually better to use exceptionToError instead.
Strings produced by show are usually not very user-friendly.
Note: goes via String, so not efficient.
exceptionToError :: Exception exc => exc -> Error Source #
Create an error from an Exception type.
The default implementation of displayException is show, so the same user-friendliness problems of showToError apply.
Note: goes via String, so not efficient.
Adding context
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.
Pretty printing
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"
Unsafe unwrapping
Sometimes you want to assure that an Error could not have happened at runtime,
even though there is the possibility in the types.
In that case you can use expectError/unwrapError.
They will panic at runtime if there was an error.
expectError should usually be preffered, since it adds a context message.
These are modelled after Result::expect()
and Result::unwrap() in the rust stdlib.
expectError :: HasCallStack => 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.
Panics: if Error
Example:
>>>expectError "something bad happened" $ Left (newError "oh no!")*** Exception: something bad happened: oh no! ...
>>>expectError "something bad happened" $ Right 4242
unwrapError :: HasCallStack => Either Error p -> p Source #