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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use std::fmt;
use reqwest::Error as HttpClientError;
use reqwest::StatusCode;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
AuthenticationFailed,
AccessDenied,
ResourceNotFound,
TooManyItems,
EndpointNotFound,
InvalidInput,
IncompatibleApiVersion,
Conflict,
OperationTimedOut,
OperationFailed,
ProtocolError,
InvalidResponse,
InternalServerError,
InvalidConfig,
}
#[derive(Debug, Clone)]
pub struct Error {
kind: ErrorKind,
message: String,
status: Option<StatusCode>,
}
impl Error {
#[inline]
pub fn new<S: Into<String>>(kind: ErrorKind, message: S) -> Error {
Error {
kind,
message: message.into(),
status: None,
}
}
#[inline]
pub fn set_status(&mut self, status: StatusCode) {
self.status = Some(status);
}
#[inline]
pub fn with_status(mut self, status: StatusCode) -> Self {
self.set_status(status);
self
}
#[inline]
pub fn kind(&self) -> ErrorKind {
self.kind
}
pub(crate) fn new_endpoint_not_found<D: fmt::Display>(service_type: D) -> Error {
Error::new(
ErrorKind::EndpointNotFound,
format!("Endpoint for service {} was not found", service_type),
)
}
}
impl ErrorKind {
#[allow(clippy::trivially_copy_pass_by_ref)]
#[inline]
pub fn description(&self) -> &'static str {
match self {
ErrorKind::AuthenticationFailed => "Failed to authenticate",
ErrorKind::AccessDenied => "Access to the resource is denied",
ErrorKind::ResourceNotFound => "Requested resource was not found",
ErrorKind::TooManyItems => "Request returned too many items",
ErrorKind::EndpointNotFound => "Requested endpoint was not found",
ErrorKind::InvalidInput => "Input value(s) are invalid or missing",
ErrorKind::IncompatibleApiVersion => "Incompatible or unsupported API version",
ErrorKind::Conflict => "Requested cannot be fulfilled due to a conflict",
ErrorKind::OperationTimedOut => "Time out reached while waiting for the operation",
ErrorKind::OperationFailed => "Requested operation has failed",
ErrorKind::ProtocolError => "Error when accessing the server",
ErrorKind::InvalidResponse => "Received invalid response",
ErrorKind::InternalServerError => "Internal server error or bad gateway",
ErrorKind::InvalidConfig => "configuration file cannot be found or is invalid",
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: {}", self.kind, self.message)
}
}
impl ::std::error::Error for Error {
fn description(&self) -> &str {
self.kind.description()
}
fn cause(&self) -> Option<&dyn ::std::error::Error> {
None
}
}
impl From<StatusCode> for ErrorKind {
fn from(value: StatusCode) -> ErrorKind {
match value {
StatusCode::UNAUTHORIZED => ErrorKind::AuthenticationFailed,
StatusCode::FORBIDDEN => ErrorKind::AccessDenied,
StatusCode::NOT_FOUND => ErrorKind::ResourceNotFound,
StatusCode::NOT_ACCEPTABLE => ErrorKind::IncompatibleApiVersion,
StatusCode::CONFLICT => ErrorKind::Conflict,
c if c.is_client_error() => ErrorKind::InvalidInput,
c if c.is_server_error() => ErrorKind::InternalServerError,
_ => ErrorKind::InvalidResponse,
}
}
}
impl From<HttpClientError> for Error {
fn from(value: HttpClientError) -> Error {
let msg = value.to_string();
let kind = if value.is_builder() {
ErrorKind::InvalidInput
} else {
value
.status()
.map(From::from)
.unwrap_or(ErrorKind::ProtocolError)
};
let error = Error::new(kind, msg);
if let Some(status) = value.status() {
error.with_status(status)
} else {
error
}
}
}
#[cfg(test)]
pub mod test {
use super::{Error, ErrorKind};
#[test]
fn test_error_display() {
let error = Error::new(ErrorKind::InvalidInput, "boom");
assert_eq!(error.kind(), ErrorKind::InvalidInput);
let s = format!("{}", error);
assert_eq!(&s, "Input value(s) are invalid or missing: boom");
}
}