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
use serde::Deserialize;
use std::fmt::{Display, Formatter};
use thiserror::Error as ThisError;
#[derive(Clone, Debug, Deserialize)]
pub struct ApiError {
pub code: String,
pub text: String,
}
impl Display for ApiError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "API error: (code: {}): {}", self.code, self.text)
}
}
#[non_exhaustive]
#[derive(ThisError, Debug)]
pub enum Error {
#[cfg(feature = "from-reqwest")]
#[error("HTTP error")]
HttpError(#[from] reqwest::Error),
#[cfg(feature = "from-reqwest")]
#[error("Invalid header value")]
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
#[cfg(feature = "from-serde_json")]
#[error("JSON error")]
JsonError(#[from] serde_json::Error),
#[cfg(feature = "from-tokio")]
#[error("Unable to get request lock")]
LockError(#[from] tokio::sync::AcquireError),
#[error("The etag for this request is missing or invalid")]
InvalidEtag,
#[error("Unable to get token `{0}`")]
TokenError(String),
#[error("Heading levels must be between 1 and 6, '{0}' was provided")]
InvalidHeadingLevel(u32),
#[error("You're not logged in")]
NotLoggedIn,
#[error("You're not logged in as a bot account")]
NotLoggedInAsBot,
#[error("Missing permission: {0}")]
PermissionsError(String),
#[error("Page does not exist: {0}")]
PageDoesNotExist(String),
#[error("Page [[{0}]] is protected: {1}")]
ProtectedPage(String, String),
#[error("Edit conflict on {0}")]
EditConflict(String),
#[error("{0}")]
ApiError(ApiError),
#[error("Unknown error: {0}")]
UnknownError(String),
}
impl Error {
pub fn is_save_error(&self) -> bool {
matches!(self, Error::ProtectedPage(_, _) | Error::EditConflict(_))
}
}
impl From<ApiError> for Error {
fn from(apierr: ApiError) -> Self {
match apierr.code.as_str() {
"assertuserfailed" => Self::NotLoggedIn,
"assertbotfailed" => Self::NotLoggedInAsBot,
_ => Self::ApiError(apierr),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_save_error() {
assert!(Error::EditConflict("foo".to_string()).is_save_error());
assert!(!Error::UnknownError("bar".to_string()).is_save_error());
}
#[test]
fn test_from_apierror() {
let apierr = ApiError {
code: "assertbotfailed".to_string(),
text: "Something something".to_string(),
};
let err = Error::from(apierr);
if let Error::NotLoggedInAsBot = err {
assert!(true);
} else {
panic!("Expected NotLoggedInAsBot error");
}
}
#[test]
fn test_to_string() {
let apierr = ApiError {
code: "errorcode".to_string(),
text: "Some description".to_string(),
};
assert_eq!(
&apierr.to_string(),
"API error: (code: errorcode): Some description"
);
}
}