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
use bytes::Bytes;
use http::{StatusCode, Uri};
use std::{
error::Error as StdError,
fmt::{self, Display, Formatter},
};
pub use crate::DecodeBodyError;
pub use http::Error as HttpError;
pub use hyper::Error as HyperError;
pub use std::io::Error as IoError;
pub use tokio_tungstenite::tungstenite::Error as SocketError;
pub type ClientResult<T> = Result<T, ClientError>;
#[derive(Debug)]
pub enum ClientError {
FailedRequestBuilder(HttpError),
Http(HyperError),
EndpointError {
raw_error: Bytes,
status: StatusCode,
endpoint: Uri,
},
SocketError(SocketError),
MessageDecode(DecodeBodyError),
ContentNotSupported(Bytes),
InvalidUrl(InvalidUrlKind),
Io(IoError),
}
impl Display for ClientError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
ClientError::FailedRequestBuilder(err) => {
write!(f, "error occured while building a request: {}", err)
}
ClientError::Http(err) => {
write!(f, "an error occured within the HTTP client: {}", err)
}
ClientError::EndpointError {
raw_error,
status,
endpoint,
} => write!(
f,
"endpoint {} returned an error with status code {}: {:?}",
endpoint, status, raw_error,
),
ClientError::SocketError(err) => {
write!(f, "an error occured within the websocket: {}", err)
}
ClientError::ContentNotSupported(_) => {
write!(f, "server responded with a non protobuf response")
}
ClientError::MessageDecode(err) => write!(
f,
"failed to decode response data as protobuf response: {}",
err
),
ClientError::InvalidUrl(err) => write!(f, "invalid base URL: {}", err),
ClientError::Io(err) => write!(f, "io error: {}", err),
}
}
}
impl From<hyper::Error> for ClientError {
fn from(err: hyper::Error) -> Self {
ClientError::Http(err)
}
}
impl From<DecodeBodyError> for ClientError {
fn from(err: DecodeBodyError) -> Self {
ClientError::MessageDecode(err)
}
}
impl From<SocketError> for ClientError {
fn from(err: SocketError) -> Self {
ClientError::SocketError(err)
}
}
impl From<IoError> for ClientError {
fn from(err: IoError) -> Self {
ClientError::Io(err)
}
}
impl From<tower_http::BodyOrIoError<hyper::Error>> for ClientError {
fn from(err: tower_http::BodyOrIoError<hyper::Error>) -> Self {
match err {
tower_http::BodyOrIoError::Body(err) => err.into(),
tower_http::BodyOrIoError::Io(err) => err.into(),
}
}
}
impl StdError for ClientError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
ClientError::InvalidUrl(err) => Some(err),
ClientError::MessageDecode(err) => Some(err),
ClientError::Http(err) => Some(err),
ClientError::SocketError(err) => Some(err),
ClientError::Io(err) => Some(err),
ClientError::FailedRequestBuilder(err) => Some(err),
_ => None,
}
}
}
#[derive(Debug)]
pub enum InvalidUrlKind {
InvalidScheme,
}
impl Display for InvalidUrlKind {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
InvalidUrlKind::InvalidScheme => {
write!(f, "invalid scheme, expected `http` or `https`")
}
}
}
}
impl StdError for InvalidUrlKind {}