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 KeepAliveExpire,
12 RequestTimeout,
14 Closed,
15 Service(S),
17 Body(B),
19 Io(io::Error),
21 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}