Skip to main content

xitca_http/
error.rs

1//! error types.
2
3use core::{convert::Infallible, error::Error, fmt};
4
5use tracing::error;
6
7use super::http::Version;
8
9pub(crate) use super::tls::TlsError;
10
11/// HttpService layer error.
12pub enum HttpServiceError<S, B> {
13    Ignored,
14    Service(S),
15    Body(B),
16    Timeout(TimeoutError),
17    UnSupportedVersion(Version),
18    Tls(TlsError),
19    #[cfg(feature = "http1")]
20    H1(super::h1::Error<S, B>),
21    // Http/2 error happen in HttpService handle.
22    #[cfg(feature = "http2")]
23    H2(super::h2::Error<S, B>),
24    // Http/3 error happen in HttpService handle.
25    #[cfg(feature = "http3")]
26    H3(super::h3::Error<S, B>),
27}
28
29impl<S, B> fmt::Debug for HttpServiceError<S, B>
30where
31    S: fmt::Debug,
32    B: fmt::Debug,
33{
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match *self {
36            Self::Ignored => f.write_str("Error detail is ignored."),
37            Self::Service(ref e) => fmt::Debug::fmt(e, f),
38            Self::Timeout(ref timeout) => write!(f, "{timeout:?} is timed out"),
39            Self::UnSupportedVersion(ref protocol) => write!(f, "Protocol: {protocol:?} is not supported"),
40            Self::Body(ref e) => fmt::Debug::fmt(e, f),
41            Self::Tls(ref e) => fmt::Debug::fmt(e, f),
42            #[cfg(feature = "http1")]
43            Self::H1(ref e) => fmt::Debug::fmt(e, f),
44            #[cfg(feature = "http2")]
45            Self::H2(ref e) => fmt::Debug::fmt(e, f),
46            #[cfg(feature = "http3")]
47            Self::H3(ref e) => fmt::Debug::fmt(e, f),
48        }
49    }
50}
51
52impl<S, B> HttpServiceError<S, B>
53where
54    S: fmt::Debug,
55    B: fmt::Debug,
56{
57    pub fn log(self, target: &str) {
58        // TODO: add logging for different error types.
59        error!(target = target, ?self);
60    }
61}
62
63/// time out error from async task that run for too long.
64#[derive(Debug)]
65pub enum TimeoutError {
66    TlsAccept,
67}
68
69impl<S, B> From<()> for HttpServiceError<S, B> {
70    fn from(_: ()) -> Self {
71        Self::Ignored
72    }
73}
74
75impl<S, B> From<Infallible> for HttpServiceError<S, B> {
76    fn from(e: Infallible) -> Self {
77        match e {}
78    }
79}
80
81/// Default Request/Response body error.
82pub type BodyError = Box<dyn Error + Send + Sync>;