Skip to main content

ErrorChainExt

Trait ErrorChainExt 

Source
pub trait ErrorChainExt {
    // Required methods
    fn chain(&self) -> Chain<'_> ;
    fn root_cause(&self) -> &(dyn StdError + 'static);
}
Expand description

Walk an error and its source chain.

Import via use oopsie::prelude::* or use oopsie::ErrorChainExt.

Implemented for every E: Error + 'static and for dyn Error + 'static itself, so it covers a typed #[oopsie] error, a Welp, and the &(dyn Error + 'static) returned by source with a single method call.

§Example

use oopsie::{Oopsie, ErrorChainExt as _, ResultExt as _};

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum ReadError {
    #[oopsie("read failed: {source}")]
    Read { source: std::io::Error },
}

let err: Result<(), std::io::Error> = Err(std::io::Error::other("disk full"));
let err = err.context(Read).unwrap_err();

let messages: Vec<String> = err.chain().map(ToString::to_string).collect();
assert_eq!(messages, ["read failed: disk full", "disk full"]);
assert_eq!(err.root_cause().to_string(), "disk full");

Required Methods§

Source

fn chain(&self) -> Chain<'_>

Iterate this error followed by its transitive source causes.

The first item is the error itself (anyhow::Chain semantics), so the chain is never empty.

Source

fn root_cause(&self) -> &(dyn StdError + 'static)

The last error in the chain: the deepest source, or this error itself when it has none.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ErrorChainExt for dyn StdError + 'static

Source§

fn chain(&self) -> Chain<'_>

Source§

fn root_cause(&self) -> &(dyn StdError + 'static)

Implementors§

Source§

impl<E: StdError + 'static> ErrorChainExt for E