Error providers
The zero-sized providers that raise and wrap a context's abstract error, without depending on which concrete error type the context chose.
Overview
Context-Generic Programming (CGP) enables pluggable trait implementations in Rust, and the
error providers supply common error-handling strategies to any
context, the type the implementation runs against.
CanRaiseError and
CanWrapError define what a context can do with an error:
turn a source error into its abstract Self::Error, or attach detail to one it already holds. They
say nothing about how. These providers supply the how for the cases that need no particular error
library. Each one is generic over the context, so it works with whatever error type the context's
HasErrorType names.
Each provider implements one or both of two provider traits, all reached through cgp::extra::error
and none in the prelude. CanRaiseError's provider trait is ErrorRaiser, wired with
ErrorRaiserComponent. CanWrapError's provider trait is ErrorWrapper, wired with
ErrorWrapperComponent. A provider that implements ErrorRaiser supplies raising; one that
implements ErrorWrapper supplies wrapping; two of them supply both.
These are the in-tree counterparts to the standalone backends in cgp-error-anyhow, cgp-error-eyre,
and cgp-error-std. Those crates supply providers tied to one concrete error type, such as raising into
an anyhow::Error. The providers here stay abstract over the context's error type and capture
strategies that hold for any of them. A context usually wires a mix: a backend for the concrete error
type, plus these generic providers for the cross-cutting strategies.
The seven providers, by role
The seven divide into three groups.
The pure raisers convert a source error into the abstract error:
RaiseFromconverts a source error through the standardFromtrait.ReturnErrorhandles the case where the source error already is the context's error, so raising is the identity.RaiseInfallibleabsorbscore::convert::Infallible, an error that can never be constructed.
The string-formatting providers implement both components by formatting through a string:
DebugErrorformats the source or detail with theDebugtrait, then forwards to the context's ownStringhandling.DisplayErrordoes the same with theDisplaytrait.
The remaining two act on the wrapping and raising components directly:
DiscardDetailwraps by throwing the detail away and returning the error unchanged.PanicOnErroraborts with the source error's debug output rather than producing an error value.
Wiring them
A context gains an error strategy by wiring one of these providers to ErrorRaiserComponent or
ErrorWrapperComponent, both imported from cgp::core::error, and the provider's where clause
decides when that wiring type-checks. Because both components dispatch on the source-error or detail
type, a context commonly wires several providers at once, one per source error type. The modern form
is the open statement of delegate_components!; the error
family is still defined with #[derive_delegate], so a nested
UseDelegate table is the form you will read in existing code.
The string-formatting providers compose with the rest rather than replacing them. DebugError and
DisplayError turn a value into a String and raise or wrap it through the context's own String
key, so the context wires one concrete rule for String, such as RaiseFrom, and routes the other
source types to the formatters. The String key itself must never go to a formatter, since the
formatter would then forward to itself (E0275).
Related constructs
CanRaiseErrorandCanWrapError— the components these implement, through theErrorRaiserandErrorWrapperprovider traits.HasErrorType— names the abstractSelf::Errorevery provider here produces.UseDelegate— the legacy table that dispatches these providers per source-error type, which theopenstatement replaces.delegate_components!andcheck_components!— wire and verify them.
The ideas behind it:
- Modular error handling — how the abstract error type, the raise and wrap components, and these strategies fit together as interchangeable wiring decisions.
Source
- The providers are in
cgp-error-extra:impls/. - The
ErrorRaiserandErrorWrapperprovider traits and the consumer traits they build on are incgp-error. - The concrete backends are in
crates/standalone/error/.
An AI agent wrote this page using the CGP knowledge base. Its content was verified against the library's source. See How AI is used in this project.