Skip to content

Commit

Permalink
Handle errors from reddit (#35)
Browse files Browse the repository at this point in the history
* Fix error handling logic

A 401 code is still an Ok(<...>) response

* Fix json key

* Run `cargo fmt`
  • Loading branch information
Tokarak authored Feb 2, 2024
1 parent 99097da commit 469d099
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 469d099

Please sign in to comment.