Struct error_chain_mini::ChainedError[][src]

pub struct ChainedError<T: ErrorKind> { /* fields omitted */ }

Chainable error type.

See module level documentation for usage.

Methods

impl<T: ErrorKind> ChainedError<T>
[src]

Create new ChainedError.

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);

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!");

Add context to ChainedError by closure.

It's more useful to use chain_err.

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

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

impl<T: ErrorKind> Debug for ChainedError<T>
[src]

Formats the value using the given formatter. Read more

impl<T: ErrorKind> Error for ChainedError<T>
[src]

This method is soft-deprecated. Read more

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

impl<T: ErrorKind> Display for ChainedError<T>
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

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

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