Errore
This library provides a framework to handle and trace errors across modules and crates.
At the moment errore is in development and breaking changes are to be expected.
Example
auth.rs
use ;
// if 'error::result::Result' is not needed, a simple wildcard import can be used:
// use errore::*;
use *;
/// Errors for any failed authentication.
// Automatically generated:
// pub struct Ec(pub Span<Error>)
account.rs
use *;
use crateauth;
/// Errors for account related operations.
// Automatically generated:
// pub struct Ec(pub Span<Error>)
main.rs
use *;
Examplary error output:
Error: example_basic::account::Authentication
├─▶ <example_basic::auth::ReadPassword> Invalid email or password
│ ├╴ examples/basic/src/auth.rs:20:8
│ ╰╴ examples/basic/src/auth.rs:24:8
│
╰─▶ <example_basic::account::Authentication>
╰╴ examples/basic/src/account.rs:20:5
Trace records:
<example_basic::auth::ReadPassword> Invalid email or password at examples/basic/src/auth.rs:20:8
<example_basic::auth::ReadPassword> Invalid email or password at examples/basic/src/auth.rs:24:8
<example_basic::account::Authentication> Invalid email or password at examples/basic/src/account.rs:20:5
Error display:
example_basic::account::Authentication: Invalid email or password
at examples/basic/src/auth.rs:20:8
Error extraction with 'match':
OS error code 2: entity not found
Error extraction with 'get()':
OS error code 2: entity not found
For more examples please see here.
Features
- Tracing capability with rich metadata such as file location and line number without
backtrace - Generates trait implementations for metadata and error conversion
- Customizable
SubscriberandFormatterinterface - Support for user attached data with
Extensionsat subscriber - Partial API compatibility with
thiserrorthat allows to optionally enableerrorein public distributed libraries. Seeexample - Usable in application and library code
no-stdsupport &wasmcompatible
Limitations & Disadvantages
- Invasive code changes with
Resultinstrumentation are required - Nightly compiler is required
- Only one error per module can be defined
- No recursive or self-referencing fields
- Error conversion with attribute macro
#fromrequires a trait implementation ofstd::error::Errorfor the type - Generics with traits in error fields need to be declared with the
wherekeyword - Some edge cases cannot be expressed with generics (for e.g. nesting)
- No
anyhowsupport (shouldn't be a problem iferroreis used)
Recommendations
- For public libraries an optional feature flag for errore is advisable.
For the best results
thiserrorshould be used. See Example - For private libraries
errorecan be used as is. Errors are best declared on a per module basis. See Example - For general best-practices with
errorethe various examples can serve as a good foundation
Feature flags
ctor: Utilizes link_sections provided by thectorandinventorycrates to offer a better implementation of the metadata and subscriber relevant code. The fallback implementation is based on lazy static variables. This feature can be disabled atno-stdprojects on build failures.debug-no-std: Enables internal debug logging with thedefmtcrate.debug-std: Enables internal debug logging with thelogcrate.std: Enables standard library support. If thestdfeature is not enabled, thealloccrate is required.
Thanks to
- @dtolnay - Maintainer of several great crates including
thiserrorwhich is used as errore`s foundation - tracing / error-stack / error_set maintainers & contributors for the inspiring codebase and ideas