From 27b56c17813389e05cf49334d4ad892ca45f6463 Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Sun, 21 Jul 2024 14:22:54 -0400 Subject: [PATCH] fix(client): handle new gated and quarantined error types (#187) * fix(client): handle new gated and quarantined error types * test(client): add test for gated and quarantined --- src/client.rs | 8 ++++++++ src/subreddit.rs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/client.rs b/src/client.rs index bc07168..6e73a59 100644 --- a/src/client.rs +++ b/src/client.rs @@ -382,6 +382,14 @@ pub async fn json(path: String, quarantine: bool) -> Result { let () = force_refresh_token().await; return Err("OAuth token has expired. Please refresh the page!".to_string()); } + // Handle quarantined + if json["reason"] == "quarantined" { + return Err("quarantined".into()); + } + // Handle gated + if json["reason"] == "gated" { + return Err("gated".into()); + } Err(format!("Reddit error {} \"{}\": {} | {path}", json["error"], json["reason"], json["message"])) } else { Ok(json) diff --git a/src/subreddit.rs b/src/subreddit.rs index 6ea65d7..5d05f96 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -515,3 +515,11 @@ async fn test_fetching_subreddit() { let subreddit = subreddit("rust", false).await; assert!(subreddit.is_ok()); } + +#[tokio::test(flavor = "multi_thread")] +async fn test_gated_and_quarantined() { + let quarantined = subreddit("edgy", true).await; + assert!(quarantined.is_ok()); + let gated = subreddit("drugs", true).await; + assert!(gated.is_ok()); +}