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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! Module to contain code related to errors that could be produced by the API.
use core::fmt::{Debug, Display};
use std::{
hash::{Hash, Hasher},
io,
sync::Arc,
time::SystemTimeError,
};
/// Alias for a Result with the error type ytmapi-rs::Error.
pub type Result<T> = core::result::Result<T, Error>;
/// This type represents all errors this API could produce.
pub struct Error {
// This is boxed to avoid passing around very large errors - in the case of an Api error we
// want to provide the source file to the caller.
inner: Box<ErrorKind>,
}
/// The kind of the error.
/// This list may grow over time, and it's not recommended to exhaustively match
/// on it.
#[non_exhaustive]
pub enum ErrorKind {
/// Error from HTTP client.
Web { message: String },
/// General io error.
// TODO: improve
Io(io::Error),
/// Expected array at `key` to contain a minimum number of elements.
ArraySize {
/// The target path (JSON pointer notation) that we tried to parse.
key: String,
/// The source json from Innertube that we were trying to parse.
// NOTE: API could theoretically produce multiple errors referring to the same source json.
// Hence reference counted, Arc particularly to ensure Error is thread safe.
json: Arc<String>,
/// The minimum number of expected elements.
min_elements: usize,
},
/// Expected the array at `key` to contain a `target_path`
PathNotFoundInArray {
/// The target path (JSON pointer notation) that we tried to parse.
key: String,
/// The source json from Innertube that we were trying to parse.
// NOTE: API could theoretically produce multiple errors referring to the same source json.
// Hence reference counted, Arc particularly to ensure Error is thread safe.
json: Arc<String>,
/// The path (JSON pointer notation) we tried to find in the elements of
/// the array.
target_path: String,
},
/// Expected `key` to contain at least one of `target_paths`
PathsNotFound {
/// The path (JSON pointer notation) that we tried to parse.
key: String,
/// The source json from Innertube that we were trying to parse.
// NOTE: API could theoretically produce multiple errors referring to the same source json.
// Hence reference counted, Arc particularly to ensure Error is thread safe.
json: Arc<String>,
/// The paths (JSON pointer notation) we tried to find.
target_paths: Vec<String>,
},
// TODO: Consider adding query type to error.
/// Field of the JSON file was not in the expected format (e.g expected an
/// array).
Parsing {
/// The target path (JSON pointer notation) that we tried to parse.
key: String,
/// The source json from Innertube that we were trying to parse.
// NOTE: API could theoretically produce multiple errors referring to the same source json.
// Hence reference counted, Arc particularly to ensure Error is thread safe.
json: Arc<String>,
/// The format we were trying to parse into.
target: ParseTarget,
/// The message we received from the parser, if any.
//TODO: Include in ParseTarget.
message: Option<String>,
},
/// Expected key did not occur in the JSON file.
Navigation {
/// The target path (JSON pointer notation) that we tried to parse.
key: String,
/// The source json from Innertube.
// NOTE: API could theoretically produce multiple errors referring to the same source json.
// Hence reference counted, Arc particularly to ensure Error is thread safe.
json: Arc<String>,
},
/// Received a response from InnerTube that was not in the expected (JSON)
/// format.
InvalidResponse { response: String },
/// InnerTube credential header not in expected format.
Header,
UnableToSerializeGoogleOAuthToken {
response: String,
err: serde_json::Error,
},
/// InnerTube rejected the User Agent we are using.
InvalidUserAgent(String),
/// Failed to authenticate using Browse Auth credentials (may have expired,
/// or been incorrectly provided).
BrowserAuthenticationFailed,
/// OAuthToken has expired.
/// Returns a hash of the expired token generated using the default hasher.
OAuthTokenExpired { token_hash: u64 },
// This is a u64 not a usize as that is what serde_json will deserialize to.
// TODO: Could use a library to handle these.
/// Recieved an error code in the Json reply from InnerTube.
OtherErrorCodeInResponse { code: u64, message: String },
/// Innertube returned a STATUS_FAILED for the query.
ApiStatusFailed,
/// Unable to obtain system time for the query to Innertube.
SystemTimeError { message: String },
}
/// The type we were attempting to pass from the Json.
#[derive(Debug, Clone)]
pub enum ParseTarget {
Array,
Other(String),
}
impl Error {
/// Extract the inner kind from the error for pattern matching.
pub fn into_kind(self) -> ErrorKind {
*self.inner
}
/// If an error is a Navigation or Parsing error, return the source Json and
/// key at the location of the error.
pub fn get_json_and_key(&self) -> Option<(String, &String)> {
match self.inner.as_ref() {
ErrorKind::Navigation { json, key } => Some((json.to_string(), key)),
ErrorKind::Parsing { json, key, .. } => Some((json.to_string(), key)),
ErrorKind::PathNotFoundInArray { key, json, .. } => Some((json.to_string(), key)),
ErrorKind::PathsNotFound { key, json, .. } => Some((json.to_string(), key)),
ErrorKind::ArraySize { key, json, .. } => Some((json.to_string(), key)),
ErrorKind::Web { .. }
| ErrorKind::Io(_)
| ErrorKind::InvalidResponse { .. }
| ErrorKind::Header
| ErrorKind::ApiStatusFailed
| ErrorKind::UnableToSerializeGoogleOAuthToken { .. }
| ErrorKind::OtherErrorCodeInResponse { .. }
| ErrorKind::OAuthTokenExpired { .. }
| ErrorKind::BrowserAuthenticationFailed
| ErrorKind::SystemTimeError { .. }
| ErrorKind::InvalidUserAgent(_) => None,
}
}
pub(crate) fn invalid_user_agent<S: Into<String>>(user_agent: S) -> Self {
Self {
inner: Box::new(ErrorKind::InvalidUserAgent(user_agent.into())),
}
}
pub(crate) fn oauth_token_expired(token: &crate::auth::OAuthToken) -> Self {
let mut h = std::hash::DefaultHasher::new();
token.hash(&mut h);
let token_hash = h.finish();
Self {
inner: Box::new(ErrorKind::OAuthTokenExpired { token_hash }),
}
}
pub(crate) fn browser_authentication_failed() -> Self {
Self {
inner: Box::new(ErrorKind::BrowserAuthenticationFailed),
}
}
pub(crate) fn navigation<S: Into<String>>(key: S, json: Arc<String>) -> Self {
Self {
inner: Box::new(ErrorKind::Navigation {
key: key.into(),
json,
}),
}
}
pub(crate) fn array_size(
key: impl Into<String>,
json: Arc<String>,
min_elements: usize,
) -> Self {
let key = key.into();
Self {
inner: Box::new(ErrorKind::ArraySize {
key,
json,
min_elements,
}),
}
}
pub(crate) fn path_not_found_in_array(
key: impl Into<String>,
json: Arc<String>,
target_path: impl Into<String>,
) -> Self {
let key = key.into();
let target_path = target_path.into();
Self {
inner: Box::new(ErrorKind::PathNotFoundInArray {
key,
json,
target_path,
}),
}
}
pub(crate) fn paths_not_found(
key: impl Into<String>,
json: Arc<String>,
target_paths: Vec<String>,
) -> Self {
let key = key.into();
Self {
inner: Box::new(ErrorKind::PathsNotFound {
key,
json,
target_paths,
}),
}
}
pub(crate) fn parsing<S: Into<String>>(
key: S,
json: Arc<String>,
target: ParseTarget,
message: Option<String>,
) -> Self {
Self {
inner: Box::new(ErrorKind::Parsing {
key: key.into(),
json,
target,
message,
}),
}
}
pub(crate) fn header() -> Self {
Self {
inner: Box::new(ErrorKind::Header),
}
}
pub(crate) fn response<S: Into<String>>(response: S) -> Self {
let response = response.into();
Self {
inner: Box::new(ErrorKind::InvalidResponse { response }),
}
}
pub(crate) fn unable_to_serialize_oauth<S: Into<String>>(
response: S,
err: serde_json::Error,
) -> Self {
let response = response.into();
Self {
inner: Box::new(ErrorKind::UnableToSerializeGoogleOAuthToken { response, err }),
}
}
pub(crate) fn other_code(code: u64, message: String) -> Self {
Self {
inner: Box::new(ErrorKind::OtherErrorCodeInResponse { code, message }),
}
}
pub(crate) fn status_failed() -> Self {
Self {
inner: Box::new(ErrorKind::ApiStatusFailed),
}
}
pub(crate) fn web(message: impl Into<String>) -> Self {
Self {
inner: Box::new(ErrorKind::Web {
message: message.into(),
}),
}
}
}
impl std::error::Error for Error {}
impl Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorKind::Web { message } => write!(f, "Web error <{message}> received."),
ErrorKind::Io(e) => write!(f, "IO error {e} recieved."),
ErrorKind::Header => write!(f, "Error parsing header."),
ErrorKind::InvalidResponse { response } => {
write!(
f,
"Response is invalid json - unable to deserialize. <{response}>"
)
}
ErrorKind::OtherErrorCodeInResponse { code, message } => {
write!(
f,
"Http error code {code} recieved in response. Message: <{message}>."
)
}
ErrorKind::PathsNotFound {
key, target_paths, ..
} => write!(
f,
"Expected {key} to contain one of the following paths: {:?}",
target_paths
),
ErrorKind::PathNotFoundInArray {
key, target_path, ..
} => write!(f, "Expected {key} to contain a {target_path}"),
ErrorKind::Navigation { key, json: _ } => {
write!(f, "Key {key} not found in Api response.")
}
ErrorKind::ArraySize {
key,
json: _,
min_elements,
} => {
write!(
f,
"Expected {key} to contain at least {min_elements} elements."
)
}
ErrorKind::Parsing {
key,
json: _,
target,
message,
} => write!(
f,
"Error {:?}. Unable to parse into {:?} at {key}",
message, target
),
ErrorKind::ApiStatusFailed => write!(f, "Api returned STATUS_FAILED for the query"),
ErrorKind::OAuthTokenExpired { token_hash: _ } => write!(f, "OAuth token has expired"),
ErrorKind::InvalidUserAgent(u) => write!(f, "InnerTube rejected User Agent {u}"),
ErrorKind::BrowserAuthenticationFailed => write!(f, "Browser authentication failed"),
ErrorKind::UnableToSerializeGoogleOAuthToken { response, err } => write!(
f,
"Unable to serialize Google auth token {}, received error {}",
response, err
),
ErrorKind::SystemTimeError { message } => write!(
f,
"Error obtaining system time to use in API query. <{message}>"
),
}
}
}
// As this is displayed when unwrapping, we don't want to end up including the
// entire format of this struct (potentially including entire source json file).
impl Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// TODO: Improve implementation
Display::fmt(&*self.inner, f)
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&*self.inner, f)
}
}
impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
let message = err.to_string();
Self {
inner: Box::new(ErrorKind::Web { message }),
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self {
inner: Box::new(ErrorKind::Io(err)),
}
}
}
impl From<SystemTimeError> for Error {
fn from(err: SystemTimeError) -> Self {
let message = err.to_string();
Self {
inner: Box::new(ErrorKind::SystemTimeError { message }),
}
}
}
impl From<ErrorKind> for Error {
fn from(value: ErrorKind) -> Self {
Self {
inner: Box::new(value),
}
}
}