Expand description
This crate is for error handling in Rust programs, providing an Err
struct which represents
an error with a reason.
The type of this reason is any, but typically an enum variant is used. The name of this enum variant indicates the reason for the error, and its fields store contextual information about the situation in which the error occurred. Since the path of an enum variant, including its package, is unique within a program, the enum variant representing the reason is useful for identifying the specific error, locating where it occurred, or generating appropriate error messages, etc.
Optionally, by using notify
feature and registering error handlers in advance, it is possible
to receive notifications either synchronously or asynchronously at the time the error struct is
created.
§Install
In Cargo.toml
, write this crate as a dependency:
[dependencies]
errs = "0.2.0"
If you want to use error notification, specifies notify
or full
in the dependency features:
[dependencies]
errs = { version = "0.2.0", features = ["notify"] }
§Usage
§Err instantiation and identification of a reason
The Err
struct can be instantiated with new<R>(reason: R)
function or
with_source<R, E>(reason: R, source: E)
function.
Then, the reason can be identified with reason<R>(&self)
method and a match
statement,
or match_reason<R>(&self, func fn(&R))
method.
The following code is an example which uses new<R>(reason: R)
function for instantiation,
and reason<R>(&self)
method and a match
statement for identifying a reason:
use errs::Err;
#[derive(Debug)]
enum Reasons {
IllegalState { state: String },
// ...
}
let err = Err::new(Reasons::IllegalState { state: "bad state".to_string() });
match err.reason::<Reasons>() {
Ok(r) => match r {
Reasons::IllegalState { state } => println!("state = {state}"),
_ => { /* ... */ }
}
Err(err) => match err.reason::<String>() {
Ok(s) => println!("string reason = {s}"),
Err(err) => { /* ... */ }
}
}
§Notification of Err instantiations
This crate optionally provides a feature to notify pre-registered error handlers when an Err
is instantiated.
Multiple error handlers can be registered, and you can choose to receive notifications either
synchronously or asynchronously.
To register error handlers that receive notifications synchronously, use the
add_sync_err_handler
function.
For asynchronous notifications, use the add_async_err_handler
function.
Error notifications will not occur until the fix_err_handlers
function is called.
This function locks the current set of error handlers, preventing further additions and
enabling notification processing.
errs::add_async_err_handler(|err, tm| {
println!("{}:{}:{} - {}", tm, err.file, err.line, err);
});
errs::add_sync_err_handler(|info, tm| {
// ...
});
errs::fix_err_handlers();
Structs§
- Err
- Is the struct that represents an error with a reason.
Functions§
- add_
async_ err_ handler notify
- Adds a new asynchronous error handler to the global handler list.
- add_
sync_ err_ handler notify
- Adds a new synchronous error handler to the global handler list.
- fix_
err_ handlers notify
- Prevents modification of the error handler lists.