Trait error_iter::ErrorIter
source · [−]pub trait ErrorIter: Error + Sized + 'static {
fn sources(&self) -> ErrorIterator<'_>ⓘNotable traits for ErrorIterator<'a>impl<'a> Iterator for ErrorIterator<'a> type Item = &'a (dyn Error + 'static); { ... }
}Expand description
Implement this trait on your error types for free iterators over their sources!
The default implementation provides iterators for any type that implements std::error::Error.
Provided Methods
sourcefn sources(&self) -> ErrorIterator<'_>ⓘNotable traits for ErrorIterator<'a>impl<'a> Iterator for ErrorIterator<'a> type Item = &'a (dyn Error + 'static);
fn sources(&self) -> ErrorIterator<'_>ⓘNotable traits for ErrorIterator<'a>impl<'a> Iterator for ErrorIterator<'a> type Item = &'a (dyn Error + 'static);
Create an iterator over the error and its recursive sources.
use error_iter::ErrorIter;
use thiserror::Error;
#[derive(Debug, Error)]
enum Error {
#[error("Nested error: {0}")]
Nested(#[source] Box<Error>),
#[error("Leaf error")]
Leaf,
}
impl ErrorIter for Error {}
let error = Error::Nested(Box::new(Error::Leaf));
let mut iter = error.sources();
assert_eq!("Nested error: Leaf error".to_string(), iter.next().unwrap().to_string());
assert_eq!("Leaf error".to_string(), iter.next().unwrap().to_string());
assert!(iter.next().is_none());
assert!(iter.next().is_none());