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
use hyper::{
header::InvalidHeaderValue, http::Error as HttpError, Error as HyperError, StatusCode,
};
use serde::Deserialize;
use serde_json::Error as SerdeError;
use std::{error::Error as StdError, fmt};
use url::ParseError;
#[derive(Debug, Deserialize)]
pub struct ApiError {
pub error: Option<String>,
}
impl StdError for ApiError {}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.error {
Some(ref msg) => f.write_str(msg),
None => f.write_str("empty error message"),
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum OsuError {
BodyError { source: HttpError },
BuilderMissingId,
BuilderMissingSecret,
ChunkingResponse { source: HyperError },
CreatingTokenHeader { source: InvalidHeaderValue },
NotFound,
NoToken,
Parsing { body: String, source: SerdeError },
ParsingValue { source: ParsingError },
Request { source: HyperError },
RequestTimeout,
Response {
body: String,
source: ApiError,
status: StatusCode,
},
ServiceUnavailable(String),
UnavailableEndpoint,
UpdateToken { source: Box<OsuError> },
Url {
source: ParseError,
url: String,
},
}
impl StdError for OsuError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::BodyError { source } => Some(source),
Self::BuilderMissingId => None,
Self::BuilderMissingSecret => None,
Self::ChunkingResponse { source } => Some(source),
Self::CreatingTokenHeader { source } => Some(source),
Self::NotFound => None,
Self::NoToken => None,
Self::Parsing { source, .. } => Some(source),
Self::ParsingValue { source } => Some(source),
Self::Request { source } => Some(source),
Self::RequestTimeout => None,
Self::Response { source, .. } => Some(source),
Self::ServiceUnavailable(_) => None,
Self::UnavailableEndpoint => None,
Self::UpdateToken { source } => Some(source),
Self::Url { source, .. } => Some(source),
}
}
}
impl fmt::Display for OsuError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BodyError { .. } => f.write_str("failed to create request body"),
Self::BuilderMissingId => {
f.write_str("failed to build osu client, no client id was provided")
}
Self::BuilderMissingSecret => {
f.write_str("failed to build osu client, no client secret was provided")
}
Self::ChunkingResponse { .. } => f.write_str("failed to chunk the response"),
Self::CreatingTokenHeader { .. } => {
f.write_str("failed to parse token for authorization header")
}
Self::NotFound => f.write_str(
"the osu!api returned a 404 implying a missing score, incorrect name, id, etc",
),
Self::NoToken => f.write_str(
"The previous osu!api token expired and the client \
has not yet succeeded in acquiring a new one. \
Can not send requests until a new token has been acquired. \
This should only occur during an extended downtime of the osu!api.",
),
Self::Parsing { body, .. } => write!(f, "failed to deserialize response: {}", body),
Self::ParsingValue { .. } => f.write_str("failed to parse value"),
Self::Request { .. } => f.write_str("failed to send request"),
Self::RequestTimeout => f.write_str("osu!api did not respond in time"),
Self::Response { status, .. } => write!(f, "response error, status {}", status),
Self::ServiceUnavailable(body) => write!(
f,
"osu!api may be temporarily unavailable (received 503): {}",
body
),
Self::UnavailableEndpoint => {
f.write_str("the endpoint is not available for the client's authorization level")
}
Self::UpdateToken { .. } => f.write_str("failed to update osu!api token"),
Self::Url { url, .. } => write!(f, "failed to parse URL of a request; url: `{}`", url),
}
}
}
impl From<HttpError> for OsuError {
fn from(e: HttpError) -> Self {
Self::BodyError { source: e }
}
}
impl From<ParsingError> for OsuError {
fn from(e: ParsingError) -> Self {
Self::ParsingValue { source: e }
}
}
#[derive(Debug)]
pub enum ParsingError {
Genre(u8),
Grade(String),
Language(u8),
ModsU32(u32),
ModsStr(String),
RankStatus(i8),
ScoringType(u8),
Team(u8),
TeamType(u8),
}
impl StdError for ParsingError {}
impl fmt::Display for ParsingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Genre(n) => write!(f, "failed to parse {} into Genre", n),
Self::Grade(s) => write!(f, "failed to parse `{}` into Grade", s),
Self::Language(n) => write!(f, "failed to parse {} into Language", n),
Self::ModsU32(n) => write!(f, "failed to parse {} into GameMods", n),
Self::ModsStr(s) => write!(f, "failed to parse `{}` into GameMods", s),
Self::RankStatus(n) => write!(f, "failed to parse {} into RankStatus", n),
Self::ScoringType(n) => write!(f, "failed to parse {} into ScoringType", n),
Self::Team(n) => write!(f, "failed to parse {} into Team", n),
Self::TeamType(n) => write!(f, "failed to parse {} into TeamType", n),
}
}
}