xitca_http/h1/
error.rs

1use core::fmt;
2
3use std::io;
4
5use crate::error::HttpServiceError;
6
7use super::proto::error::ProtoError;
8
9pub enum Error<S, B> {
10    /// socket keep-alive timer expired.
11    KeepAliveExpire,
12    /// socket fail to receive a complete request head in given time window.
13    RequestTimeout,
14    Closed,
15    /// service error. terminate connection right away.
16    Service(S),
17    /// service response body error. terminate connection right away.
18    Body(B),
19    /// socket and/or runtime error. terminate connection right away.
20    Io(io::Error),
21    /// http/1 protocol error. transform into http response and send to client.
22    /// after which the connection can be gracefully shutdown or kept open.
23    Proto(ProtoError),
24}
25
26impl<S, B> fmt::Debug for Error<S, B>
27where
28    S: fmt::Debug,
29    B: fmt::Debug,
30{
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match *self {
33            Self::KeepAliveExpire => f.write_str("Keep-Alive time expired"),
34            Self::RequestTimeout => f.write_str("request head time out"),
35            Self::Closed => f.write_str("closed"),
36            Self::Service(ref e) => fmt::Debug::fmt(e, f),
37            Self::Body(ref e) => fmt::Debug::fmt(e, f),
38            Self::Io(ref e) => fmt::Debug::fmt(e, f),
39            Self::Proto(ref e) => fmt::Debug::fmt(e, f),
40        }
41    }
42}
43
44impl<S, B> From<ProtoError> for Error<S, B> {
45    fn from(e: ProtoError) -> Self {
46        Self::Proto(e)
47    }
48}
49
50impl<S, B> From<io::Error> for Error<S, B> {
51    fn from(e: io::Error) -> Self {
52        use io::ErrorKind;
53        match e.kind() {
54            ErrorKind::ConnectionReset | ErrorKind::UnexpectedEof | ErrorKind::WriteZero => Self::Closed,
55            ErrorKind::WouldBlock | ErrorKind::Interrupted => {
56                unreachable!("non-blocking I/O must not emit WouldBlock/Interrupted error")
57            }
58            _ => Self::Io(e),
59        }
60    }
61}
62
63impl<S, B> From<Error<S, B>> for HttpServiceError<S, B> {
64    fn from(e: Error<S, B>) -> Self {
65        match e {
66            Error::Service(e) => HttpServiceError::Service(e),
67            e => Self::H1(e),
68        }
69    }
70}