xs/
error.rs

1pub type Error = Box<dyn std::error::Error + Send + Sync>;
2
3#[derive(Debug)]
4pub struct NotFound;
5
6impl std::fmt::Display for NotFound {
7    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8        write!(f, "Not found")
9    }
10}
11
12impl std::error::Error for NotFound {}
13
14impl NotFound {
15    /// Check if an error is our custom NotFound error
16    pub fn is_not_found(err: &Error) -> bool {
17        err.downcast_ref::<NotFound>().is_some()
18    }
19}
20
21/// Check if an error has ErrorKind::NotFound in its chain
22pub fn has_not_found_io_error(err: &Error) -> bool {
23    // Check if it's directly an IO error with NotFound kind
24    if let Some(io_err) = err.downcast_ref::<std::io::Error>() {
25        return io_err.kind() == std::io::ErrorKind::NotFound;
26    }
27
28    // Check the error chain for IO errors with NotFound kind
29    let mut source = err.source();
30    while let Some(err) = source {
31        if let Some(io_err) = err.downcast_ref::<std::io::Error>() {
32            if io_err.kind() == std::io::ErrorKind::NotFound {
33                return true;
34            }
35        }
36        source = err.source();
37    }
38
39    false
40}