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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::fmt::{Debug, Display, Formatter};
use std::string::FromUtf8Error;
use http::StatusCode;
use crate::{Body, InMemoryResponse, InMemoryResponseExt, Response};

pub type Result<T = Response, E = Error> = std::result::Result<T, E>;
pub type InMemoryError = Error<InMemoryResponse>;
pub type InMemoryResult<T> = Result<T, InMemoryError>;
pub type ProtocolResult<T> = Result<T, ProtocolError>;


#[derive(Debug)]
pub enum ProtocolError {
    ConnectionError(hyper::Error),
    Utf8Error(FromUtf8Error),
    JsonError(serde_json::Error),
    IoError(std::io::Error),
    TooManyRedirects,
    TooManyRetries,
}

impl std::error::Error for ProtocolError {}

impl Display for ProtocolError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ProtocolError::ConnectionError(e) => write!(f, "ConnectionError: {}", e),
            ProtocolError::Utf8Error(e) => write!(f, "Utf8Error: {}", e),
            ProtocolError::JsonError(e) => write!(f, "JsonError: {}", e),
            ProtocolError::IoError(e) => write!(f, "IoError: {}", e),
            ProtocolError::TooManyRedirects => write!(f, "TooManyRedirects"),
            ProtocolError::TooManyRetries => write!(f, "TooManyRetries"),
        }
    }
}

#[derive(Debug)]
pub enum Error<T = Response> {
    Protocol(ProtocolError),
    HttpError(T),
}

impl Error {
    /// Get the error status code.
    pub fn status(&self) -> Option<StatusCode> {
        match self {
            Error::HttpError(r) => Some(r.status()),
            _ => None,
        }
    }

    pub async fn into_content(self) -> InMemoryError {
        match self {
            Error::HttpError(r) => {
                let (parts, body) = r.into_parts();
                let content_type = parts.headers.get(http::header::CONTENT_TYPE);
                let body = match body.into_content_type(content_type).await {
                    Ok(body) => body,
                    Err(e) => return e.into(),
                };
                Error::HttpError(InMemoryResponse::from_parts(parts, body))
            }
            Error::Protocol(e) => Error::Protocol(e),
        }
    }
}

impl InMemoryError {
    pub fn transform_error<T>(self) -> Error<T>
        where
            T: TryFrom<InMemoryResponse>,
            T::Error: Into<Error<T>>,
    {
        match self {
            InMemoryError::Protocol(e) => Error::Protocol(e),
            InMemoryError::HttpError(e) => match e.try_into() {
                Ok(r) => Error::HttpError(r),
                Err(e) => e.into(),
            }
        }
    }

    pub fn into_text(self) -> String {
        match self {
            InMemoryError::Protocol(e) => e.to_string(),
            InMemoryError::HttpError(r) => {
                r.text().unwrap_or_else(|e| format!("Error reading body as text: {}", e))
            }
        }
    }
}

impl From<InMemoryError> for Error {
    fn from(value: InMemoryError) -> Self {
        match value {
            Error::HttpError(r) => {
                let (parts, body) = r.into_parts();
                let body: Body = body.into();
                let r = crate::Response::from_parts(parts, body);
                Error::HttpError(r)
            },
            Error::Protocol(e) => Error::Protocol(e),
        }
    }
}

impl<T: Debug> Display for Error<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::HttpError(r) => write!(f, "HttpError {{ res: {:?} }}", r),
            Error::Protocol(p) => write!(f, "ProtocolError: {}", p),
        }
    }
}

impl<T: Debug> std::error::Error for Error<T> {}

impl serde::de::Error for Error {
    fn custom<T: Display>(msg: T) -> Self {
        Error::Protocol(ProtocolError::JsonError(serde_json::Error::custom(&msg.to_string())))
    }
}

impl<T> From<serde_json::Error> for Error<T> {
    fn from(value: serde_json::Error) -> Self {
        Error::Protocol(ProtocolError::JsonError(value))
    }
}

impl From<std::io::Error> for ProtocolError {
    fn from(value: std::io::Error) -> Self {
        ProtocolError::IoError(value)
    }
}

impl<T> From<hyper::Error> for Error<T> {
    fn from(value: hyper::Error) -> Self {
        Error::Protocol(ProtocolError::ConnectionError(value))
    }
}

impl<T> From<FromUtf8Error> for Error<T> {
    fn from(value: FromUtf8Error) -> Self {
        Error::Protocol(ProtocolError::Utf8Error(value))
    }
}

impl<T> From<ProtocolError> for Error<T> {
    fn from(value: ProtocolError) -> Self {
        Error::Protocol(value)
    }
}

impl From<hyper::Error> for ProtocolError {
    fn from(value: hyper::Error) -> Self {
        Self::ConnectionError(value)
    }
}

impl From<serde_json::Error> for ProtocolError {
    fn from(value: serde_json::Error) -> Self {
        Self::JsonError(value)
    }
}

impl From<FromUtf8Error> for ProtocolError {
    fn from(value: FromUtf8Error) -> Self {
        Self::Utf8Error(value)
    }
}