Skip to content

Commit

Permalink
Add error handling for ip-ban responses
Browse files Browse the repository at this point in the history
If you are ip banned by the boomlings servers, they (or rather,
Cloudflare) will respond with `error code: 1005`. So handle this as an
error case.

Signed-off-by: stadust <[email protected]>
  • Loading branch information
stadust committed May 5, 2024
1 parent 8cdbb85 commit 18c465e
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub enum ResponseError<'a> {
/// The response was not worked in the expected way (too few sections, etc.)
#[error("unexpected format")]
UnexpectedFormat,

#[error("you have been IP banned by Cloudflare")]
IpBanned,
}

impl<'a> From<DeError<'a>> for ResponseError<'a> {
Expand All @@ -51,9 +54,7 @@ impl<'a> From<DeError<'a>> for ResponseError<'a> {
}

pub fn parse_get_gj_levels_response(response: &str) -> Result<Vec<ListedLevel>, ResponseError> {
if response == "-1" {
return Err(ResponseError::NotFound);
}
check_response_errors(response)?;

let mut sections = response.split('#');

Expand Down Expand Up @@ -112,27 +113,21 @@ pub fn parse_get_gj_levels_response(response: &str) -> Result<Vec<ListedLevel>,
}

pub fn parse_download_gj_level_response(response: &str) -> Result<Level, ResponseError> {
if response == "-1" {
return Err(ResponseError::NotFound);
}
check_response_errors(response)?;

let mut sections = response.split('#');

Ok(Level::from_gj_str(section!(sections))?)
}

pub fn parse_get_gj_user_info_response(response: &str) -> Result<Profile, ResponseError> {
if response == "-1" {
return Err(ResponseError::NotFound);
}
check_response_errors(response)?;

Ok(Profile::from_gj_str(response)?)
}

pub fn parse_get_gj_users_response(response: &str) -> Result<SearchedUser, ResponseError> {
if response == "-1" {
return Err(ResponseError::NotFound);
}
check_response_errors(response)?;

let mut sections = response.split('#');

Expand All @@ -143,9 +138,7 @@ pub fn parse_get_gj_users_response(response: &str) -> Result<SearchedUser, Respo
}

pub fn parse_get_gj_comments_response(response: &str) -> Result<Vec<LevelComment>, ResponseError> {
if response == "-1" {
return Err(ResponseError::NotFound);
}
check_response_errors(response)?;

let mut sections = response.split('#');

Expand Down Expand Up @@ -175,9 +168,7 @@ pub fn parse_get_gj_comments_response(response: &str) -> Result<Vec<LevelComment
}

pub fn parse_get_gj_acccount_comments_response(response: &str) -> Result<Vec<ProfileComment>, ResponseError> {
if response == "-1" {
return Err(ResponseError::NotFound);
}
check_response_errors(response)?;

let mut sections = response.split('#');

Expand All @@ -186,3 +177,15 @@ pub fn parse_get_gj_acccount_comments_response(response: &str) -> Result<Vec<Pro
.map(|fragment| Ok(ProfileComment::from_gj_str(fragment)?))
.collect()
}

fn check_response_errors(response: &str) -> Result<(), ResponseError> {
if response == "-1" {
return Err(ResponseError::NotFound)
}

if response == "error code: 1005" {
return Err(ResponseError::IpBanned)
}

Ok(())
}

0 comments on commit 18c465e

Please sign in to comment.