From 2e89a858586c694ba8531fcc11049f110fcd7300 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Tue, 9 Mar 2021 22:23:26 -0800 Subject: [PATCH] Handle alternative status codes --- src/utils.rs | 52 +++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 6b42513..1b01305 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -514,7 +514,7 @@ pub async fn error(req: Request<()>, msg: String) -> tide::Result { } #[async_recursion] -async fn connect(path: String) -> io::Result<(i16, String)> { +async fn connect(path: String) -> io::Result { // Build reddit-compliant user agent for Libreddit let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION")); @@ -557,7 +557,7 @@ async fn connect(path: String) -> io::Result<(i16, String)> { .collect::>()[1]; connect(location.replace("https://www.reddit.com", "")).await } else { - Ok((status, body)) + Ok(body) } } @@ -572,35 +572,29 @@ pub async fn request(path: String) -> Result { }; match connect(path).await { - Ok((status, body)) => { - match status { - // If response is success - 200 => { - // Parse the response from Reddit as JSON - let parsed: Result = from_str(&body); - match parsed { - Ok(json) => { - // 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!("{} - Error parsing reddit error", url); - "Error parsing reddit error" - }) - }) - .to_string(), - ) - } else { - Ok(json) - } - } - Err(e) => err("Failed to parse page JSON data", e.to_string()), + Ok(body) => { + // Parse the response from Reddit as JSON + let parsed: Result = from_str(&body); + match parsed { + Ok(json) => { + // 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!("{} - Error parsing reddit error", url); + "Error parsing reddit error" + }) + }) + .to_string(), + ) + } else { + Ok(json) } } - _ => err("Couldn't send request to Reddit", status.to_string()), + Err(e) => err("Failed to parse page JSON data", e.to_string()), } } Err(e) => err("Couldn't send request to Reddit", e.to_string()),