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:
Nazar 2024-02-02 19:53:15 +00:00 committed by GitHub
parent 99097da6b8
commit 469d0994f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -320,28 +320,19 @@ pub async fn json(path: String, quarantine: bool) -> Result<Value, String> {
let json: Value = value; let json: Value = value;
// If Reddit returned an error // If Reddit returned an error
if json["error"].is_i64() { if json["error"].is_i64() {
Err( // OAuth token has expired; http status 401
json["reason"] if json["message"] == "Unauthorized" {
.as_str() error!("Forcing a token refresh");
.unwrap_or_else(|| { let () = force_refresh_token().await;
json["message"].as_str().unwrap_or_else(|| { return Err("OAuth token has expired. Please refresh the page!".to_string());
eprintln!("{REDDIT_URL_BASE}{path} - Error parsing reddit error"); }
"Error parsing reddit error" Err(format!("Reddit error {} \"{}\": {}", json["error"], json["reason"], json["message"]))
})
})
.to_string(),
)
} else { } else {
Ok(json) Ok(json)
} }
} }
Err(e) => { Err(e) => {
error!("Got a bad response from reddit {e}. Status code: {status}"); error!("Got an invalid response from reddit {e}. Status code: {status}");
// Unauthorized; token expired
if status == 401 {
error!("Forcing a token refresh");
let () = force_refresh_token().await;
}
if status.is_server_error() { if status.is_server_error() {
Err("Reddit is having issues, check if there's an outage".to_string()) Err("Reddit is having issues, check if there's an outage".to_string())
} else { } else {