Expand description
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
At first glance, errore its error definition looks quite similar to thiserror`s:
However, it is possible to extract additional information from the error, as shown in this sample 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 foundThe complete example can be seen here. For other examples please see here.
§Features
Tracingcapability with rich metadata such as file location and line number withoutbacktrace- Generates trait implementations for
metadataand error conversion - Customizable
SubscriberandFormatterinterface - Support for user attached data with
Extensionsat subscriber - Partial API compatibility with
thiserrorthat allows to optionally enableerrorein public distributed libraries on stable rust.
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 can be declared on a per module basis or as one global type.
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 logging with thedefmtcrate to debugerroreitself.debug-std: Enables internal logging with thelogcrate to debugerroreitself.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
Modules§
Macros§
- err
- Shorthand macro for
Result::Errin order to create an error context from a related enum or struct type. - formatter
- Sets a global formatter for errors.
- subscriber
- Registers an error subscriber. Multiple subscribers can be submitted.
Structs§
- Downcasted
- A construct to represent a reference to a downcasted error.
- Extensions
- An immutable, read-only reference to a Context’s extensions.
- Extensions
Mut - An mutable reference to a Context’s extensions.
- Id
- Represents an identifier hash.
- Location
- A struct containing information about the location of the caller.
- Trace
Context - A context for holding data used for error tracing.
- Trace
Context Builder - This builder allows to configure an
TraceContextobject. - Trace
Record - The record represents an entity where an error was created, propagated or converted in the error chain.
Traits§
- Extension
- Marks an object as extendable with user supplied extensions.
- Extract
- Provides access to the trace context for an object.
- Extractable
- Marker trait for extractable errors.
- Metadata
- Defines the metadata for an error type.
- Trace
Access - Interface to interact with the collected traces.
- Traceable
- Interface to implement tracing related functionality.