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
use std::any;
use std::error::Error;
use thiserror::Error;
use crate::api::PaginationError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum BodyError {
#[error("failed to URL encode form parameters: {}", source)]
UrlEncoded {
#[from]
source: serde_urlencoded::ser::Error,
},
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ApiError<E>
where
E: Error + Send + Sync + 'static,
{
#[error("client error: {}", source)]
Client {
source: E,
},
#[error("failed to parse url: {}", source)]
UrlParse {
#[from]
source: url::ParseError,
},
#[error("failed to create form data: {}", source)]
Body {
#[from]
source: BodyError,
},
#[error("could not parse JSON response: {}", source)]
Json {
#[from]
source: serde_json::Error,
},
#[error("gitlab server error: {}", msg)]
Gitlab {
msg: String,
},
#[error("gitlab server error: {:?}", obj)]
GitlabObject {
obj: serde_json::Value,
},
#[error("gitlab server error: {:?}", obj)]
GitlabUnrecognized {
obj: serde_json::Value,
},
#[error("could not parse {} data from JSON: {}", typename, source)]
DataType {
source: serde_json::Error,
typename: &'static str,
},
#[error("failed to handle for pagination: {}", source)]
Pagination {
#[from]
source: PaginationError,
},
}
impl<E> ApiError<E>
where
E: Error + Send + Sync + 'static,
{
pub fn client(source: E) -> Self {
ApiError::Client {
source,
}
}
pub(crate) fn from_gitlab(value: serde_json::Value) -> Self {
let error_value = value
.pointer("/message")
.or_else(|| value.pointer("/error"));
if let Some(error_value) = error_value {
if let Some(msg) = error_value.as_str() {
ApiError::Gitlab {
msg: msg.into(),
}
} else {
ApiError::GitlabObject {
obj: error_value.clone(),
}
}
} else {
ApiError::GitlabUnrecognized {
obj: value,
}
}
}
pub(crate) fn data_type<T>(source: serde_json::Error) -> Self {
ApiError::DataType {
source,
typename: any::type_name::<T>(),
}
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use thiserror::Error;
use crate::api::ApiError;
#[derive(Debug, Error)]
#[error("my error")]
enum MyError {}
#[test]
fn gitlab_error_error() {
let obj = json!({
"error": "error contents",
});
let err: ApiError<MyError> = ApiError::from_gitlab(obj);
if let ApiError::Gitlab {
msg,
} = err
{
assert_eq!(msg, "error contents");
} else {
panic!("unexpected error: {}", err);
}
}
#[test]
fn gitlab_error_message_string() {
let obj = json!({
"message": "error contents",
});
let err: ApiError<MyError> = ApiError::from_gitlab(obj);
if let ApiError::Gitlab {
msg,
} = err
{
assert_eq!(msg, "error contents");
} else {
panic!("unexpected error: {}", err);
}
}
#[test]
fn gitlab_error_message_object() {
let err_obj = json!({
"blah": "foo",
});
let obj = json!({
"message": err_obj,
});
let err: ApiError<MyError> = ApiError::from_gitlab(obj);
if let ApiError::GitlabObject {
obj,
} = err
{
assert_eq!(obj, err_obj);
} else {
panic!("unexpected error: {}", err);
}
}
#[test]
fn gitlab_error_message_unrecognized() {
let err_obj = json!({
"some_weird_key": "an even weirder value",
});
let err: ApiError<MyError> = ApiError::from_gitlab(err_obj.clone());
if let ApiError::GitlabUnrecognized {
obj,
} = err
{
assert_eq!(obj, err_obj);
} else {
panic!("unexpected error: {}", err);
}
}
}