Struct ChainedError

Source
pub struct ChainedError<T: ErrorKind> { /* private fields */ }
Expand description

Chainable error type.

See module level documentation for usage.

Implementations§

Source§

impl<T: ErrorKind> ChainedError<T>

Source

pub fn new(kind: T, context: Vec<Box<dyn ErrorContext>>) -> Self

Create new ChainedError.

Source

pub fn kind(&self) -> &T

Returns a reference of Error Kind

§Usage
#[derive(Debug, ErrorKind, Eq, PartialEq)]
enum ErrorType {
    A,
    B,
    C
}
let chained = ErrorType::B.into_err();
assert_eq!(*chained.kind(), ErrorType::B);
Source

pub fn contexts(&self) -> impl Iterator<Item = &str>

Returns a reference of Error Contexts Returns a reference of Error Kind

§Usage
#[derive(Debug, ErrorKind, Eq, PartialEq)]
enum ErrorType {
    A,
    B,
    C
}
let chained = ErrorType::B.into_with(|| "Error is Caused!");
let chained = chained.chain(|| "Error is Chained!");
let mut cxts = chained.contexts();
assert_eq!(cxts.next().unwrap(), "Error is Caused!");
assert_eq!(cxts.next().unwrap(), "Error is Chained!");
Source

pub fn chain<C, F>(self, op: F) -> Self
where C: ErrorContext, F: FnOnce() -> C,

Add context to ChainedError by closure.

It’s more useful to use chain_err.

Source

pub fn convert<U>(self) -> ChainedError<U>
where U: ErrorKind + From<T>,

Convert ChainedError<T> into ChainedError<U> using std::convert::from.

§Usage
mod external {
    #[derive(ErrorKind, Eq, PartialEq, Debug)]
    #[msg(short = "error in external")]
    pub struct ExtErrorKind;
    pub type Error = ChainedError<ExtErrorKind>;
    pub fn func() -> Result<(), Error> {
        Err(ExtErrorKind{}.into_with(|| "In external::func()"))
    }
}
use external::{self, ExtErrorKind};
#[derive(ErrorKind, Eq, PartialEq, Debug)]
enum MyErrorKind {
    Internal,
    #[msg(short = "from mod 'external'", detailed = "{:?}", _0)]
    External(ExtErrorKind),
};
impl From<ExtErrorKind> for MyErrorKind {
    fn from(e: ExtErrorKind) -> MyErrorKind {
        MyErrorKind::External(e)
    }
}
type Error = ChainedError<MyErrorKind>;
let chained: Result<(), Error>
    = external::func().map_err(|e| e.convert().chain(|| "In my_func()"));
if let Err(chained) = chained {
    assert_eq!(*chained.kind(), MyErrorKind::External(ExtErrorKind {}));
    assert_eq!(chained.contexts().nth(1).unwrap(), "In my_func()");
}
Source

pub fn convert_with<U, F>(self, converter: F) -> ChainedError<U>
where U: ErrorKind, F: FnOnce(T) -> U,

Convert ChainedError<T> into ChainedError<U> using closure.

§Usage
mod external {
    #[derive(ErrorKind, Eq, PartialEq, Debug)]
    #[msg(short = "error in external")]
    pub struct ExtErrorKind;
    pub type Error = ChainedError<ExtErrorKind>;
    pub fn func() -> Result<(), Error> {
        Err(ExtErrorKind{}.into_with(|| "In external::func()"))
    }
}
use external::{self, ExtErrorKind};
#[derive(ErrorKind, Eq, PartialEq, Debug)]
enum MyErrorKind {
    Internal,
    #[msg(short = "from mod 'external'", detailed = "{:?}", _0)]
    External(ExtErrorKind),
};
type Error = ChainedError<MyErrorKind>;
let chained: Result<(), Error> = external::func().map_err(|e| {
        e.convert_with(|e| MyErrorKind::External(e))
         .chain(|| "In my_func()")
   });
if let Err(chained) = chained {
    assert_eq!(*chained.kind(), MyErrorKind::External(ExtErrorKind {}));
    assert_eq!(chained.contexts().nth(1).unwrap(), "In my_func()");
}

Trait Implementations§

Source§

impl<T: ErrorKind> Debug for ChainedError<T>

Source§

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

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

impl<T: ErrorKind> Display for ChainedError<T>

Source§

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

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

impl<T: ErrorKind> Error for ChainedError<T>

Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.30.0 · Source§

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

Returns the lower-level source of this error, if any. Read more
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, request: &mut Request<'a>)

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

Auto Trait Implementations§

§

impl<T> Freeze for ChainedError<T>

§

impl<T> !RefUnwindSafe for ChainedError<T>

§

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

§

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

§

impl<T> Unpin for ChainedError<T>

§

impl<T> !UnwindSafe for ChainedError<T>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

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

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.