Handle errors from reddit (#35)
* Fix error handling logic A 401 code is still an Ok(<...>) response * Fix json key * Run `cargo fmt`
This commit is contained in:
parent
99097da6b8
commit
469d0994f1
@ -320,28 +320,19 @@ pub async fn json(path: String, quarantine: bool) -> Result<Value, String> {
|
||||
let json: Value = value;
|
||||
// If Reddit returned an error
|
||||
if json["error"].is_i64() {
|
||||
Err(
|
||||
json["reason"]
|
||||
.as_str()
|
||||
.unwrap_or_else(|| {
|
||||
json["message"].as_str().unwrap_or_else(|| {
|
||||
eprintln!("{REDDIT_URL_BASE}{path} - Error parsing reddit error");
|
||||
"Error parsing reddit error"
|
||||
})
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
// OAuth token has expired; http status 401
|
||||
if json["message"] == "Unauthorized" {
|
||||
error!("Forcing a token refresh");
|
||||
let () = force_refresh_token().await;
|
||||
return Err("OAuth token has expired. Please refresh the page!".to_string());
|
||||
}
|
||||
Err(format!("Reddit error {} \"{}\": {}", json["error"], json["reason"], json["message"]))
|
||||
} else {
|
||||
Ok(json)
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Got a bad response from reddit {e}. Status code: {status}");
|
||||
// Unauthorized; token expired
|
||||
if status == 401 {
|
||||
error!("Forcing a token refresh");
|
||||
let () = force_refresh_token().await;
|
||||
}
|
||||
error!("Got an invalid response from reddit {e}. Status code: {status}");
|
||||
if status.is_server_error() {
|
||||
Err("Reddit is having issues, check if there's an outage".to_string())
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user