1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Utilities for working with `std::error::Error` on stable Rust. (Requires `std` feature.)

pub struct ErrorIter<'a> {
    inner: Option<&'a (dyn std::error::Error + 'static)>,
}

/// Create an iterator over the chained error sources.
///
/// `dyn Error iter_sources()` is a nightly-only feature. Approximate it so we can build on stable.
///
/// ```
/// use sonant::errors::iter_sources;
/// # use std::io::{Error, ErrorKind};
///
/// # let error = Error::new(ErrorKind::Other, "oh no!");
/// eprintln!("Error: {}", error);
/// for source in iter_sources(&error) {
///     eprintln!("Caused by: {}", source);
/// }
/// ```
pub fn iter_sources<E>(error: &E) -> ErrorIter
where
    E: std::error::Error + 'static,
{
    ErrorIter { inner: Some(error) }
}

impl<'a> Iterator for ErrorIter<'a> {
    type Item = &'a (dyn std::error::Error + 'static);

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(error) = self.inner.take() {
            if let Some(source) = error.source() {
                self.inner = Some(source);
                return Some(source);
            }
        }

        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use thiserror::Error;

    #[derive(Debug, Error)]
    enum Error {
        #[error("Nested error: {0}")]
        Nested(#[source] Box<Error>),

        #[error("Leaf error")]
        Leaf,
    }

    #[test]
    fn iter_sources_ok() {
        let error = Error::Nested(Box::new(Error::Leaf));

        let mut counter = 0;

        for source in iter_sources(&error) {
            counter += 1;
            assert_eq!(format!("{}", source), "Leaf error");
        }

        assert_eq!(counter, 1);
    }
}