Skip to main content

tower_resilience_cache/
error.rs

1//! Error types for cache.
2
3use std::fmt;
4
5/// Errors that can occur in the cache.
6#[derive(Debug)]
7pub enum CacheError<E> {
8    /// The inner service returned an error.
9    Inner(E),
10}
11
12impl<E: fmt::Display> fmt::Display for CacheError<E> {
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        match self {
15            CacheError::Inner(e) => write!(f, "inner service error: {}", e),
16        }
17    }
18}
19
20impl<E: std::error::Error + 'static> std::error::Error for CacheError<E> {
21    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
22        match self {
23            CacheError::Inner(e) => Some(e),
24        }
25    }
26}
27
28impl<E> CacheError<E> {
29    /// Converts this error into the inner error.
30    pub fn into_inner(self) -> E {
31        match self {
32            CacheError::Inner(e) => e,
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_inner_error() {
43        let err = CacheError::Inner("test error");
44        assert_eq!(err.to_string(), "inner service error: test error");
45        assert_eq!(err.into_inner(), "test error");
46    }
47}