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>
impl<T: 'static + Display + Debug> Error<T>
sourcepub fn new(
kind: T,
error_cause: Option<Box<dyn StdError + Send + Sync + 'static>>,
occurrence: Option<String>
) -> Self
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
sourcepub fn root_cause(&self) -> Option<&(dyn StdError + 'static)>
pub fn root_cause(&self) -> Option<&(dyn StdError + 'static)>
return the root cause of the error chain, if any exists
sourcepub fn find_cause<U: StdError + 'static>(&self) -> Option<&U>
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());
}
}sourcepub fn find_chain_cause<U: StdError + 'static>(&self) -> Option<&Error<U>>
pub fn find_chain_cause<U: StdError + 'static>(&self) -> Option<&Error<U>>
sourcepub fn find_kind_or_cause<U: StdError + 'static>(&self) -> Option<&U>
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>();sourcepub fn kind(&self) -> &T
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!(),
}
}Trait Implementations§
source§impl<U: 'static + Display + Debug> ChainErrorDown for Error<U>
impl<U: 'static + Display + Debug> ChainErrorDown for Error<U>
source§impl<T: 'static + Display + Debug> Error for &mut Error<T>
impl<T: 'static + Display + Debug> Error for &mut Error<T>
source§fn source(&self) -> Option<&(dyn StdError + 'static)>
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
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
source§impl<T: 'static + Display + Debug> Error for Error<T>
impl<T: 'static + Display + Debug> Error for Error<T>
source§fn source(&self) -> Option<&(dyn StdError + 'static)>
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
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
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> 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