Struct chainerror::ChainError[][src]

pub struct ChainError<T> { /* fields omitted */ }

chains an inner error kind T with a causing error

Implementations

impl<T: 'static + Display + Debug> ChainError<T>[src]

pub fn new(
    kind: T,
    error_cause: Option<Box<dyn Error + Send + Sync + 'static>>,
    occurrence: Option<String>
) -> Self
[src]

Use the context() or map_context() Result methods instead of calling this directly

pub fn root_cause(&self) -> Option<&(dyn Error + 'static)>[src]

return the root cause of the error chain, if any exists

pub fn find_cause<U: Error + 'static>(&self) -> Option<&U>[src]

Find the first error cause of type U, if any exists

Examples

use chainerror::prelude::v1::*;
use std::error::Error;
use std::io;

fn do_some_io() -> Result<(), Box<dyn Error + Send + Sync>> {
    Err(io::Error::from(io::ErrorKind::NotFound))?;
    Ok(())
}

derive_str_context!(Func2Error);

fn func2() -> Result<(), Box<dyn Error + Send + Sync>> {
    let filename = "foo.txt";
    do_some_io().context(Func2Error(format!("Error reading '{}'", filename)))?;
    Ok(())
}

derive_str_context!(Func1Error);

fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
    func2().context(Func1Error("func1 error".into()))?;
    Ok(())
}

if let Err(e) = func1() {
    if let Some(f1err) = e.downcast_chain_ref::<Func1Error>() {
        assert!(f1err.find_cause::<io::Error>().is_some());

        assert!(f1err.find_chain_cause::<Func2Error>().is_some());
    }
}

pub fn find_chain_cause<U: Error + 'static>(&self) -> Option<&ChainError<U>>[src]

Find the first error cause of type ChainError<U>, if any exists

Same as find_cause, but hides the ChainError<U> implementation internals

Examples

// Instead of writing
err.find_cause::<ChainError<FooError>>();

// leave out the ChainError<FooError> implementation detail
err.find_chain_cause::<FooError>();

pub fn find_kind_or_cause<U: Error + 'static>(&self) -> Option<&U>[src]

Find the first error cause of type ChainError<U> or U, if any exists and return U

Same as find_cause and find_chain_cause, but hides the ChainError<U> implementation internals

Examples

// Instead of writing
err.find_cause::<ChainError<FooErrorKind>>();
// and/or
err.find_chain_cause::<FooErrorKind>();
// and/or
err.find_cause::<FooErrorKind>();

// leave out the ChainError<FooErrorKind> implementation detail
err.find_kind_or_cause::<FooErrorKind>();

pub fn kind(&self) -> &T[src]

Return a reference to T of ChainError<T>

Examples

use chainerror::prelude::v1::*;
use std::error::Error;
use std::io;

fn do_some_io() -> Result<(), Box<dyn Error + Send + Sync>> {
    Err(io::Error::from(io::ErrorKind::NotFound))?;
    Ok(())
}

derive_str_context!(Func2Error);

fn func2() -> Result<(), Box<dyn Error + Send + Sync>> {
    let filename = "foo.txt";
    do_some_io().context(Func2Error(format!("Error reading '{}'", filename)))?;
    Ok(())
}

#[derive(Debug)]
enum Func1ErrorKind {
    Func2,
    IO(String),
}

/// impl ::std::fmt::Display for Func1ErrorKind {…}

fn func1() -> ChainResult<(), Func1ErrorKind> {
    func2().context(Func1ErrorKind::Func2)?;
    do_some_io().context(Func1ErrorKind::IO("bar.txt".into()))?;
    Ok(())
}

if let Err(e) = func1() {
    match e.kind() {
        Func1ErrorKind::Func2 => {}
        Func1ErrorKind::IO(filename) => panic!(),
    }
}

pub fn iter(&self) -> impl Iterator<Item = &(dyn Error + 'static)>[src]

Returns an Iterator over all error causes/sources

Example

Trait Implementations

impl<U: 'static + Display + Debug> ChainErrorDown for ChainError<U>[src]

impl<T: 'static + Display + Debug> Debug for ChainError<T>[src]

impl<T: 'static + Display + Debug> Deref for ChainError<T>[src]

type Target = T

The resulting type after dereferencing.

impl<T: 'static + Display + Debug> Display for ChainError<T>[src]

impl<T: 'static + Display + Debug> Error for ChainError<T>[src]

impl<T: 'static + Display + Debug> Error for &mut ChainError<T>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for ChainError<T>[src]

impl<T> Send for ChainError<T> where
    T: Send
[src]

impl<T> Sync for ChainError<T> where
    T: Sync
[src]

impl<T> Unpin for ChainError<T> where
    T: Unpin
[src]

impl<T> !UnwindSafe for ChainError<T>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T, U> ChainErrorFrom<T> for U where
    T: Into<U>,
    U: 'static + Display + Debug
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> IntoChainError<U> for T where
    U: ChainErrorFrom<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.