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>
impl<T: ErrorKind> ChainedError<T>
Sourcepub fn new(kind: T, context: Vec<Box<dyn ErrorContext>>) -> Self
pub fn new(kind: T, context: Vec<Box<dyn ErrorContext>>) -> Self
Create new ChainedError.
Sourcepub fn kind(&self) -> &T
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);Sourcepub fn contexts(&self) -> impl Iterator<Item = &str>
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!");Sourcepub fn chain<C, F>(self, op: F) -> Selfwhere
C: ErrorContext,
F: FnOnce() -> C,
pub fn chain<C, F>(self, op: F) -> Selfwhere
C: ErrorContext,
F: FnOnce() -> C,
Add context to ChainedError by closure.
It’s more useful to use chain_err.
Sourcepub fn convert<U>(self) -> ChainedError<U>
pub fn convert<U>(self) -> ChainedError<U>
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()");
}Sourcepub fn convert_with<U, F>(self, converter: F) -> ChainedError<U>
pub fn convert_with<U, F>(self, converter: F) -> ChainedError<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>
impl<T: ErrorKind> Debug for ChainedError<T>
Source§impl<T: ErrorKind> Display for ChainedError<T>
impl<T: ErrorKind> Display for ChainedError<T>
Source§impl<T: ErrorKind> Error for ChainedError<T>
impl<T: ErrorKind> Error for ChainedError<T>
Source§fn description(&self) -> &str
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)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more