1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use core::fmt;

use std::io;

use crate::error::HttpServiceError;

use super::proto::error::ProtoError;

pub enum Error<S, B> {
    /// socket keep-alive timer expired.
    KeepAliveExpire,
    /// socket fail to receive a complete request head in given time window.
    RequestTimeout,
    Closed,
    /// service error. terminate connection right away.
    Service(S),
    /// service response body error. terminate connection right away.
    Body(B),
    /// socket and/or runtime error. terminate connection right away.
    Io(io::Error),
    /// http/1 protocol error. transform into http response and send to client.
    /// after which the connection can be gracefully shutdown or kept open.
    Proto(ProtoError),
}

impl<S, B> fmt::Debug for Error<S, B>
where
    S: fmt::Debug,
    B: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::KeepAliveExpire => f.write_str("Keep-Alive time expired"),
            Self::RequestTimeout => f.write_str("request head time out"),
            Self::Closed => f.write_str("closed"),
            Self::Service(ref e) => fmt::Debug::fmt(e, f),
            Self::Body(ref e) => fmt::Debug::fmt(e, f),
            Self::Io(ref e) => fmt::Debug::fmt(e, f),
            Self::Proto(ref e) => fmt::Debug::fmt(e, f),
        }
    }
}

impl<S, B> From<ProtoError> for Error<S, B> {
    fn from(e: ProtoError) -> Self {
        Self::Proto(e)
    }
}

impl<S, B> From<io::Error> for Error<S, B> {
    fn from(e: io::Error) -> Self {
        use io::ErrorKind;
        match e.kind() {
            ErrorKind::ConnectionReset | ErrorKind::UnexpectedEof | ErrorKind::WriteZero => Self::Closed,
            ErrorKind::WouldBlock | ErrorKind::Interrupted => {
                unreachable!("non-blocking I/O must not emit WouldBlock/Interrupted error")
            }
            _ => Self::Io(e),
        }
    }
}

impl<S, B> From<Error<S, B>> for HttpServiceError<S, B> {
    fn from(e: Error<S, B>) -> Self {
        match e {
            Error::Service(e) => HttpServiceError::Service(e),
            e => Self::H1(e),
        }
    }
}