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
use serde::Deserialize;
use std::fmt::{Display, Formatter};
use thiserror::Error as ThisError;
#[derive(Clone, Copy, Debug)]
pub enum ErrorFormat {
Html,
Wikitext,
Plaintext,
}
impl Display for ErrorFormat {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let val = match &self {
ErrorFormat::Html => "html",
ErrorFormat::Wikitext => "wikitext",
ErrorFormat::Plaintext => "plaintext",
};
write!(f, "{}", val)
}
}
impl Default for ErrorFormat {
fn default() -> Self {
Self::Plaintext
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct ApiError {
pub code: String,
pub text: String,
pub module: Option<String>,
}
impl Display for ApiError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "API error: (code: {}): {}", self.code, self.text)
}
}
#[derive(ThisError, Debug)]
pub enum Error {
#[error("HTTP error")]
HttpError(#[from] reqwest::Error),
#[error("Invalid header value")]
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
#[error("JSON error")]
JsonError(#[from] serde_json::Error),
#[error("Unable to get request lock")]
LockError(#[from] tokio::sync::AcquireError),
#[error("Unable to get token `{0}`")]
TokenError(String),
#[error("{0}")]
ApiError(ApiError),
#[error("Unknown error: {0}")]
UnknownError(String),
}