Expand description
flex-error is a lightweight Rust library that uses macros and traits
to switch between different error tracing implementations and no_std. The library currently supports 3 modes via Cargo feature flags: eyre_tracer (default), anyhow_tracer, and string_tracer (no_std).
The library separates out several concepts as traits:
ErrorDetail, ErrorTracer, and ErrorSource.
-
ErrorDetailis responsible to structured metadata information about a specific error. -
ErrorTraceris responsible for tracing error chains and backtraces. -
ErrorSourceallows generic conversion of external error types into an ErrorDetail with optional ErrorTrace.
With the separation of concerns, flex-error allows applications to easily
switch between different error reporting implementations,
such as eyre and anyhow, by implementing
ErrorTracer for the respective reporters.
flex-error defines a define_error! macro that define custom Detail
types and error types implementing ErrorSource<DefaultTracer>.
The DefaultTracer type is set globally by the feature flag, so that
application error types do not have to be over-generalized.
The trade off is that it is not possible to use multiple
ErrorTracer implementations at the same time across different crates that
use flex-error.
!
Re-exports§
pub extern crate alloc;
Modules§
Macros§
- define_
error define_error!is the main macro that implements a mini DSL to define error types usingflex-error. The DSL syntax is as follows:
Structs§
- BoxDetail
- Detail
Only - An
ErrorSourcethat only provides error details but do not provide any trace. This can typically comes from primitive error types that do not implementError. TheDetailtype is the error and the returned trace isNone. - Display
Error - An
ErrorSourcethat implementsDisplayand can be traced by error tracers implementingErrorMessageTracer. - Display
Only - NoSource
- An
ErrorSourcethat can be used to represent to lack of any error source. Both itsSourceandDetailtypes are(). This can be used for primitive errors that are not caused by any error source. - Trace
Clone - Trace
Error - An
ErrorSourcethat should implementErrorand other constraints such asSend,Sync,'static, so that it can be traced by error tracing libraries such aseyreandanyhow. Because these libraries take ownership of the source error object, the error cannot be extracted as detail at the same time. - Trace
Only - An
ErrorSourcethat contains only the error trace with no detail. This can for example be used for upstream functions that return tracers likeeyre::Reportdirectly.
Traits§
- Error
Message Tracer - An
ErrorMessageTracercan be used to generically trace any error detail that implementsDisplay. - Error
Source - A type implementing
ErrorSource<Trace>is a proxy type that provides the capability of extracting from an error source of typeSelf::Source, returning error detail of typeSelf::Detail, and an optional error tracer of typeTracer. - Error
Tracer - An error tracer implements
ErrorTracer<E>if it supports more sophisticated error tracing for an error typeE. The contraint forEdepends on the specific error tracer implementation. - StdError
Erroris a trait representing the basic expectations for error values, i.e., values of typeEinResult<T, E>.
Type Aliases§
- AsError
Detail - Type alias to
<Error as ErrorSource<Trace>>::Detail - AsError
Source - Type alias to
<Error as ErrorSource<Trace>>::Source - Default
Tracer - The
DefaultTracertype alias is used when defining error types usingdefine_error!. With the default Cargo features, or when theeyre_tracerfeature is set, this is configured to use the EyreTracer. Otherwise, it will be set to AnyhowTracer if theanyhow_tracerfeature is set. If neithereyre_tracernoranyhow_traceris set, thenDefaultTraceris set to StringTracer.