http_client_unix_domain_socket/
error.rs

1use hyper::StatusCode;
2#[cfg(feature = "json")]
3use serde::de::DeserializeOwned;
4
5/// Internal Error, wrapping other source of error.
6#[derive(Debug)]
7pub enum Error {
8    SocketConnectionInitiation(std::io::Error),
9    SocketConnectionClosed(Option<hyper::Error>),
10    Handhsake(hyper::Error),
11    RequestBuild(hyper::http::Error),
12    RequestSend(hyper::Error),
13    #[cfg(feature = "json")]
14    RequestParsing(serde_json::Error),
15    ResponseCollect(hyper::Error),
16    #[cfg(feature = "json")]
17    ResponseParsing(serde_json::Error),
18}
19impl std::fmt::Display for Error {
20    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
21        match self {
22            Error::SocketConnectionInitiation(e) => {
23                write!(f, "Failed to connect to unix stream, {}", e)
24            }
25            Error::SocketConnectionClosed(None) => {
26                write!(f, "Unix stream was closed without any error.")
27            }
28            Error::SocketConnectionClosed(Some(e)) => {
29                write!(f, "Unix stream was closed, {}", e)
30            }
31            Error::Handhsake(e) => {
32                write!(f, "Failed to do HTTP 1.0 handshaking, {}", e)
33            }
34            Error::RequestBuild(e) => {
35                write!(f, "Failed to build http request, {}", e)
36            }
37            Error::RequestSend(e) => {
38                write!(f, "Failed to send http request, {}", e)
39            }
40            #[cfg(feature = "json")]
41            Error::RequestParsing(e) => {
42                write!(f, "Failed to parse http json request, {}", e)
43            }
44            Error::ResponseCollect(e) => {
45                write!(f, "Failed to collect http request, {}", e)
46            }
47            #[cfg(feature = "json")]
48            Error::ResponseParsing(e) => {
49                write!(f, "Failed to parse http json response, {}", e)
50            }
51        }
52    }
53}
54impl std::error::Error for Error {
55    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56        match self {
57            Error::SocketConnectionInitiation(error) => Some(error),
58            Error::SocketConnectionClosed(Some(error)) => Some(error),
59            Error::SocketConnectionClosed(None) => None,
60            Error::Handhsake(error) => Some(error),
61            Error::RequestBuild(error) => Some(error),
62            Error::RequestSend(error) => Some(error),
63            #[cfg(feature = "json")]
64            Error::RequestParsing(error) => Some(error),
65            Error::ResponseCollect(error) => Some(error),
66            #[cfg(feature = "json")]
67            Error::ResponseParsing(error) => Some(error),
68        }
69    }
70}
71
72/// Error used by [crate::ClientUnix::send_request] to be able to return unsuccessful HTTP error body.
73#[derive(Debug)]
74pub enum ErrorAndResponse {
75    InternalError(Error),
76    ResponseUnsuccessful(StatusCode, Vec<u8>),
77}
78impl std::fmt::Display for ErrorAndResponse {
79    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
80        match self {
81            ErrorAndResponse::InternalError(e) => {
82                write!(f, "Internal error, {}", e)
83            }
84            ErrorAndResponse::ResponseUnsuccessful(status_code, _) => {
85                write!(
86                    f,
87                    "HTTP response was not successful, status code = {}",
88                    status_code
89                )
90            }
91        }
92    }
93}
94impl std::error::Error for ErrorAndResponse {
95    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
96        match self {
97            ErrorAndResponse::InternalError(error) => error.source(),
98            ErrorAndResponse::ResponseUnsuccessful(_, _) => None,
99        }
100    }
101}
102
103/// Error used by [crate::ClientUnix::send_request_json] to be able to return unsuccessful HTTP error typed body **(feature = json)**.
104#[cfg(feature = "json")]
105#[derive(Debug)]
106pub enum ErrorAndResponseJson<ERR: DeserializeOwned> {
107    InternalError(Error),
108    ResponseUnsuccessful(StatusCode, ERR),
109}
110#[cfg(feature = "json")]
111impl<ERR: DeserializeOwned> std::fmt::Display for ErrorAndResponseJson<ERR> {
112    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
113        match self {
114            ErrorAndResponseJson::InternalError(e) => {
115                write!(f, "Internal error, {}", e)
116            }
117            ErrorAndResponseJson::ResponseUnsuccessful(status_code, _) => {
118                write!(
119                    f,
120                    "HTTP response was not successful, status code = {}",
121                    status_code
122                )
123            }
124        }
125    }
126}
127#[cfg(feature = "json")]
128impl<ERR: DeserializeOwned + std::fmt::Debug> std::error::Error for ErrorAndResponseJson<ERR> {
129    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
130        match self {
131            ErrorAndResponseJson::InternalError(error) => error.source(),
132            ErrorAndResponseJson::ResponseUnsuccessful(_, _) => None,
133        }
134    }
135}