http_cache/
error.rs

1use std::fmt;
2
3/// Generic error type for the `HttpCache` middleware.
4pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
5
6/// A `Result` typedef to use with the [`BoxError`] type
7pub type Result<T> = std::result::Result<T, BoxError>;
8
9/// Error type for unknown http versions
10#[derive(Debug, Default, Copy, Clone)]
11pub struct BadVersion;
12
13impl fmt::Display for BadVersion {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        f.pad("Unknown HTTP version")
16    }
17}
18
19impl std::error::Error for BadVersion {}
20
21/// Error type for bad header values
22#[derive(Debug, Default, Copy, Clone)]
23pub struct BadHeader;
24
25impl fmt::Display for BadHeader {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        f.pad("Error parsing header value")
28    }
29}
30
31impl std::error::Error for BadHeader {}
32
33/// Error type for streaming operations
34#[derive(Debug)]
35pub struct StreamingError {
36    inner: BoxError,
37}
38
39impl StreamingError {
40    /// Create a new streaming error from any error type
41    pub fn new<E: Into<BoxError>>(error: E) -> Self {
42        Self { inner: error.into() }
43    }
44}
45
46impl fmt::Display for StreamingError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        write!(f, "Streaming error: {}", self.inner)
49    }
50}
51
52impl std::error::Error for StreamingError {
53    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54        Some(&*self.inner)
55    }
56}
57
58impl From<BoxError> for StreamingError {
59    fn from(error: BoxError) -> Self {
60        Self::new(error)
61    }
62}
63
64impl From<std::convert::Infallible> for StreamingError {
65    fn from(never: std::convert::Infallible) -> Self {
66        match never {}
67    }
68}