Struct chainerror::Error

source ·
pub struct Error<T> { /* private fields */ }
Expand description

chains an inner error kind T with a causing error

Implementations§

source§

impl<T: 'static + Display + Debug> Error<T>

source

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

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

source

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

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

source

pub fn find_cause<U: StdError + 'static>(&self) -> Option<&U>

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());
    }
}
source

pub fn find_chain_cause<U: StdError + 'static>(&self) -> Option<&Error<U>>

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

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

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

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

pub fn find_kind_or_cause<U: StdError + 'static>(&self) -> Option<&U>

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

Same as find_cause and find_chain_cause, but hides the Error<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>();
source

pub fn kind(&self) -> &T

Return a reference to T of Error<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!(),
    }
}
source

pub fn iter(&self) -> impl Iterator<Item = &(dyn StdError + 'static)>

Returns an Iterator over all error causes/sources

Example

Trait Implementations§

source§

impl<U: 'static + Display + Debug> ChainErrorDown for Error<U>

source§

fn is_chain<T: 'static + Display + Debug>(&self) -> bool

Test if of type Error<T>
source§

fn downcast_chain_ref<T: 'static + Display + Debug>(&self) -> Option<&Error<T>>

Downcast to a reference of Error<T>
source§

fn downcast_chain_mut<T: 'static + Display + Debug>( &mut self ) -> Option<&mut Error<T>>

Downcast to a mutable reference of Error<T>
source§

fn downcast_inner_ref<T: 'static + StdError>(&self) -> Option<&T>

Downcast to T of Error<T>
source§

fn downcast_inner_mut<T: 'static + StdError>(&mut self) -> Option<&mut T>

Downcast to T mutable reference of Error<T>
source§

impl<T: 'static + Display + Debug> Debug for Error<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: 'static + Display + Debug> Deref for Error<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T: 'static + Display + Debug> Display for Error<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: 'static + Display + Debug> Error for &mut Error<T>

source§

fn source(&self) -> Option<&(dyn StdError + 'static)>

The lower-level source of this error, if any. Read more
1.0.0 · source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
source§

fn provide<'a>(&'a self, demand: &mut Demand<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type based access to context intended for error reports. Read more
source§

impl<T: 'static + Display + Debug> Error for Error<T>

source§

fn source(&self) -> Option<&(dyn StdError + 'static)>

The lower-level source of this error, if any. Read more
1.0.0 · source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
source§

fn provide<'a>(&'a self, demand: &mut Demand<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type based access to context intended for error reports. Read more
source§

impl<T> From<T> for Error<T>where T: 'static + Display + Debug,

source§

fn from(e: T) -> Error<T>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Error<T>

§

impl<T> Send for Error<T>where T: Send,

§

impl<T> Sync for Error<T>where T: Sync,

§

impl<T> Unpin for Error<T>where T: Unpin,

§

impl<T> !UnwindSafe for Error<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<E> Provider for Ewhere E: Error + ?Sized,

source§

fn provide<'a>(&'a self, demand: &mut Demand<'a>)

🔬This is a nightly-only experimental API. (provide_any)
Data providers should implement this method to provide all values they are able to provide by using demand. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.