pub struct Exn<E: Error + Send + Sync + 'static = Untyped> { /* private fields */ }Expand description
An exception type that can hold an error tree and the call site.
While an error chain, a list, is automatically created when raise
and friends are invoked, one can also use Exn::raise_all to create an error
that has multiple causes.
§Warning: source() information is stringified and type-erased
All source() values are turned into frames, but lose their type information completely.
This is because they are only seen as reference and thus can’t be stored.
§Exn == Exn<Untyped>
Exn act’s like Box<dyn std::error::Error + Send + Sync + 'static>, but with the capability
to store a tree of errors along with their call sites.
§Visualisation
Linearized trees during display make a list of 3 children indistinguishable from 3 errors where each is the child of the other.
§Debug
- locations: ✔️
- error display: Display
- tree mode: linearized
§Debug + Alternate
- locations: ❌
- error display: Display
- tree mode: linearized
§Display
- locations: ❌
- error display: Debug
- tree mode: None
§Display + Alternate
- locations: ❌
- error display: Debug
- tree mode: verbatim
Implementations§
Source§impl<E: Error + Send + Sync + 'static> Exn<E>
impl<E: Error + Send + Sync + 'static> Exn<E>
Sourcepub fn new(error: E) -> Self
pub fn new(error: E) -> Self
Create a new exception with the given error.
This will automatically walk the source chain of the error and add them as children frames.
See also ErrorExt::raise for a fluent way to convert an error into an Exn instance.
Note that sources of error are degenerated to their string representation and all type information is erased.
Sourcepub fn raise_all<T, I>(children: I, err: E) -> Self
pub fn raise_all<T, I>(children: I, err: E) -> Self
Create a new exception with the given error and children.
Sourcepub fn raise<T: Error + Send + Sync + 'static>(self, err: T) -> Exn<T>
pub fn raise<T: Error + Send + Sync + 'static>(self, err: T) -> Exn<T>
Raise a new exception; this will make the current exception a child of the new one.
Sourcepub fn chain<T: Error + Send + Sync + 'static>(
self,
err: impl Into<Exn<T>>,
) -> Exn<E>
pub fn chain<T: Error + Send + Sync + 'static>( self, err: impl Into<Exn<T>>, ) -> Exn<E>
Use the current exception as the head of a chain, adding err to its children.
Sourcepub fn chain_all<T, I>(self, errors: I) -> Exn<E>
pub fn chain_all<T, I>(self, errors: I) -> Exn<E>
Use the current exception the head of a chain, adding errors to its children.
Sourcepub fn drain_children(&mut self) -> impl Iterator<Item = Exn> + '_
pub fn drain_children(&mut self) -> impl Iterator<Item = Exn> + '_
Drain all sources of this error as untyped Exn.
This is useful if one wants to re-organise errors, and the error layout is well known.
Sourcepub fn into_box(self) -> Box<E>
pub fn into_box(self) -> Box<E>
Discard all error context and return the underlying error in a Box.
This is useful to retain the allocation, as internally it’s also stored in a box,
when comparing it to Self::into_inner().
Sourcepub fn into_inner(self) -> E
pub fn into_inner(self) -> E
Discard all error context and return the underlying error.
This may be needed to obtain something that once again implements Error.
Note that this destroys the internal Box and moves the value back onto the stack.
Sourcepub fn into_error(self) -> Error
pub fn into_error(self) -> Error
Turn ourselves into a top-level Error that implements std::error::Error.
Sourcepub fn into_chain(self) -> ChainedError
pub fn into_chain(self) -> ChainedError
Convert this error tree into a chain of errors, breadth first, which flattens the tree but retains all type dynamic type information.
This is useful for inter-op with anyhow.
Sourcepub fn iter(&self) -> impl Iterator<Item = &Frame>
pub fn iter(&self) -> impl Iterator<Item = &Frame>
Iterate over all frames in breadth-first order. The first frame is this instance, followed by all of its children.
Sourcepub fn downcast_any_ref<T: Error + 'static>(&self) -> Option<&T>
pub fn downcast_any_ref<T: Error + 'static>(&self) -> Option<&T>
Iterate over all frames and find one that downcasts into error of type T.
Note that the search includes this instance as well.