Handle alternative status codes
This commit is contained in:
parent
e59b2b1346
commit
2e89a85858
52
src/utils.rs
52
src/utils.rs
@ -514,7 +514,7 @@ pub async fn error(req: Request<()>, msg: String) -> tide::Result {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_recursion]
|
#[async_recursion]
|
||||||
async fn connect(path: String) -> io::Result<(i16, String)> {
|
async fn connect(path: String) -> io::Result<String> {
|
||||||
// Build reddit-compliant user agent for Libreddit
|
// Build reddit-compliant user agent for Libreddit
|
||||||
let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION"));
|
let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION"));
|
||||||
|
|
||||||
@ -557,7 +557,7 @@ async fn connect(path: String) -> io::Result<(i16, String)> {
|
|||||||
.collect::<Vec<&str>>()[1];
|
.collect::<Vec<&str>>()[1];
|
||||||
connect(location.replace("https://www.reddit.com", "")).await
|
connect(location.replace("https://www.reddit.com", "")).await
|
||||||
} else {
|
} else {
|
||||||
Ok((status, body))
|
Ok(body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -572,35 +572,29 @@ pub async fn request(path: String) -> Result<Value, String> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
match connect(path).await {
|
match connect(path).await {
|
||||||
Ok((status, body)) => {
|
Ok(body) => {
|
||||||
match status {
|
// Parse the response from Reddit as JSON
|
||||||
// If response is success
|
let parsed: Result<Value, Error> = from_str(&body);
|
||||||
200 => {
|
match parsed {
|
||||||
// Parse the response from Reddit as JSON
|
Ok(json) => {
|
||||||
let parsed: Result<Value, Error> = from_str(&body);
|
// If Reddit returned an error
|
||||||
match parsed {
|
if json["error"].is_i64() {
|
||||||
Ok(json) => {
|
Err(
|
||||||
// If Reddit returned an error
|
json["reason"]
|
||||||
if json["error"].is_i64() {
|
.as_str()
|
||||||
Err(
|
.unwrap_or_else(|| {
|
||||||
json["reason"]
|
json["message"].as_str().unwrap_or_else(|| {
|
||||||
.as_str()
|
eprintln!("{} - Error parsing reddit error", url);
|
||||||
.unwrap_or_else(|| {
|
"Error parsing reddit error"
|
||||||
json["message"].as_str().unwrap_or_else(|| {
|
})
|
||||||
eprintln!("{} - Error parsing reddit error", url);
|
})
|
||||||
"Error parsing reddit error"
|
.to_string(),
|
||||||
})
|
)
|
||||||
})
|
} else {
|
||||||
.to_string(),
|
Ok(json)
|
||||||
)
|
|
||||||
} else {
|
|
||||||
Ok(json)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => err("Failed to parse page JSON data", e.to_string()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => 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()),
|
Err(e) => err("Couldn't send request to Reddit", e.to_string()),
|
||||||
|
Loading…
Reference in New Issue
Block a user