pub struct ChainedError<T: ErrorKind> { /* fields omitted */ }
Chainable error type.
See module level documentation for usage.
Returns a reference of Error Kind
#[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
#[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
.
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.
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()");
}
Formats the value using the given formatter. Read more
The lower-level cause of this error, if any. Read more
Formats the value using the given formatter. Read more