[][src]Struct chainerror::ChainError

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

chains an inner error kind T with a causing error

Methods

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

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

Use the cherr!() or mstrerr!() macro 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

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

derive_str_cherr!(Func2Error);

fn func2() -> Result<(), Box<Error>> {
    let filename = "foo.txt";
    do_some_io().map_err(mstrerr!(Func2Error, "Error reading '{}'", filename))?;
    Ok(())
}

derive_str_cherr!(Func1Error);

fn func1() -> Result<(), Box<Error>> {
    func2().map_err(mstrerr!(Func1Error, "func1 error"))?;
    Ok(())
}

fn main() {
    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

This example is not tested
// 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

This example is not tested
// 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_chain_or_kind::<FooErrorKind>();

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

Return a reference to T of ChainError<T>

Examples

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

derive_str_cherr!(Func2Error);

fn func2() -> Result<(), Box<Error>> {
    let filename = "foo.txt";
    do_some_io().map_err(mstrerr!(Func2Error, "Error reading '{}'", filename))?;
    Ok(())
}

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

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

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

fn main() {
    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> Deref for ChainError<T>[src]

type Target = T

The resulting type after dereferencing.

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

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

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

fn description(&self) -> &str
1.0.0
[src]

This method is soft-deprecated. Read more

fn cause(&self) -> Option<&dyn Error>
1.0.0
[src]

Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

The lower-level cause of this error, if any. Read more

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

fn description(&self) -> &str
1.0.0
[src]

This method is soft-deprecated. Read more

fn cause(&self) -> Option<&dyn Error>
1.0.0
[src]

Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

The lower-level cause of this error, if any. Read more

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

fn description(&self) -> &str
1.0.0
[src]

This method is soft-deprecated. Read more

fn cause(&self) -> Option<&dyn Error>
1.0.0
[src]

Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

The lower-level cause of this error, if any. Read more

Auto Trait Implementations

impl<T> !Send for ChainError<T>

impl<T> !Sync for ChainError<T>

Blanket Implementations

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

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

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

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

impl<T> From for T[src]

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

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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