http_cache_tower/
error.rs

1use http_cache;
2use std::fmt;
3
4/// Errors that can occur during HTTP caching operations
5#[derive(Debug)]
6pub enum HttpCacheError {
7    /// Cache operation failed
8    CacheError(String),
9    /// Body collection failed
10    BodyError(Box<dyn std::error::Error + Send + Sync>),
11    /// HTTP processing error
12    HttpError(Box<dyn std::error::Error + Send + Sync>),
13}
14
15impl fmt::Display for HttpCacheError {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            HttpCacheError::CacheError(msg) => write!(f, "Cache error: {msg}"),
19            HttpCacheError::BodyError(e) => {
20                write!(f, "Body processing error: {e}")
21            }
22            HttpCacheError::HttpError(e) => write!(f, "HTTP error: {e}"),
23        }
24    }
25}
26
27impl std::error::Error for HttpCacheError {
28    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
29        match self {
30            HttpCacheError::CacheError(_) => None,
31            HttpCacheError::BodyError(e) => Some(e.as_ref()),
32            HttpCacheError::HttpError(e) => Some(e.as_ref()),
33        }
34    }
35}
36
37impl From<http_cache::BoxError> for HttpCacheError {
38    fn from(error: http_cache::BoxError) -> Self {
39        HttpCacheError::HttpError(error)
40    }
41}
42
43#[cfg(feature = "streaming")]
44/// Errors that can occur during streaming HTTP cache operations
45#[derive(Debug)]
46pub enum TowerStreamingError {
47    /// Tower-specific error
48    Tower(Box<dyn std::error::Error + Send + Sync>),
49    /// HTTP cache streaming error
50    HttpCache(http_cache::StreamingError),
51    /// Other error
52    Other(Box<dyn std::error::Error + Send + Sync>),
53}
54
55#[cfg(feature = "streaming")]
56impl fmt::Display for TowerStreamingError {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        match self {
59            TowerStreamingError::Tower(e) => write!(f, "Tower error: {e}"),
60            TowerStreamingError::HttpCache(e) => {
61                write!(f, "HTTP cache streaming error: {e}")
62            }
63            TowerStreamingError::Other(e) => write!(f, "Other error: {e}"),
64        }
65    }
66}
67
68#[cfg(feature = "streaming")]
69impl std::error::Error for TowerStreamingError {
70    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71        match self {
72            TowerStreamingError::Tower(e) => Some(&**e),
73            TowerStreamingError::HttpCache(e) => Some(e),
74            TowerStreamingError::Other(e) => Some(&**e),
75        }
76    }
77}
78
79#[cfg(feature = "streaming")]
80impl From<Box<dyn std::error::Error + Send + Sync>> for TowerStreamingError {
81    fn from(error: Box<dyn std::error::Error + Send + Sync>) -> Self {
82        TowerStreamingError::Tower(error)
83    }
84}
85
86#[cfg(feature = "streaming")]
87impl From<http_cache::StreamingError> for TowerStreamingError {
88    fn from(error: http_cache::StreamingError) -> Self {
89        TowerStreamingError::HttpCache(error)
90    }
91}
92
93#[cfg(feature = "streaming")]
94impl From<TowerStreamingError> for http_cache::StreamingError {
95    fn from(val: TowerStreamingError) -> Self {
96        match val {
97            TowerStreamingError::HttpCache(e) => e,
98            TowerStreamingError::Tower(e) => http_cache::StreamingError::new(e),
99            TowerStreamingError::Other(e) => http_cache::StreamingError::new(e),
100        }
101    }
102}