Repository navigation
Version 0.2.0
The major difference here is a shift away from using a custom error type (generated using the error_shim!(...) macro in v0.1) to instead using a standard error type with better support for providing additional advice, and improved interoperability with the standard library's Error type. We've also shifted from using impl From<E> for MyErrorType as the primary means of wrapping errors to instead providing extensions for Results<T, E> which simplify wrapping errors with contextual advice (a pattern which was used extensively within codebases implementing human_errors).
What's Changed
- refactor: Redesign how we handle errors, enabling richer advice and easier extensibility by @notheotherben in #30
Before
use human_errors;
use std::fs;
human_errors::error_shim!(MyError);
fn read_config() -> Result<String, MyError> {
fs::read_to_string("config.toml").map_err(|err| {
user_with_internal(
"We could not read the configuration file.",
"Make sure that you've specified a valid config file with the --config option.",
err
)
})
}After
use human_errors::ResultExt;
use std::fs;
fn read_config() -> Result<String, human_errors::Error> {
fs::read_to_string("config.toml").wrap_err_as_user(
"We could not read the configuration file.",
&["Make sure that you've specified a valid config file with the --config option."])
}Migration Instructions
- Remove any
human_errors::error_shim!(MyErrorType)usage and replace all usage ofMyErrorTypewithhuman_errors::Error. - Replace any calls to
human_errors::description(msg)withmsg)(conversion happens automatically). - Replace any calls to
human_errors::user(msg, advice)withhuman_errors::user(msg, &[advice]). - Replace any calls to
human_errors::system(msg, advice)withhuman_errors::system(msg, &[advice]). - Replace any calls to
human_errors::user_with_internal(msg, advice, err)withhuman_errors::wrap_user(err, msg, &[advice]). - Replace any calls to
human_errors::system_with_internal(msg, advice, err)withhuman_errors::wrap_system(err, msg, &[advice]). - Replace code using the
.map_err(|e| human_errors::user_with_internal(msg, advice, e))pattern with.wrap_err_as_user(msg, &[advice]). - Replace code using the
.map_err(|e| human_errors::system_with_internal(msg, advice, e))pattern with.wrap_err_as_system(msg, &[advice]).
Removed Functionality
- We no longer support implementing custom
From<T>conversions, instead you should wrap errors in contextual advice using any of theuser,system,wrap_user, andwrap_systemmethods. - We no longer provide the ability to generate dynamic advice at runtime, relying on
&'static [&'static str]advice lists for performance reasons. Instead, you can incorporate any dynamic context into the error message using thewrap_user(...)andwrap_system(...)methods.
Full Changelog: v0.1.7...v0.2.0