link_checker: Handle non-success status codes (#897)

The can_fail_404_links() test doesn't encounter a 404 response in
actuality, since the google.comys domain doesn't resolve. When the
test is updated such that the response's status code is a 404, the
test fails because the check_url() function doesn't handle
non-success responses how the test's assertions expect. This commit
updates check_url() to handle non-success responses, treating them
much like errors.
This commit is contained in:
Sam Ford 2019-12-23 03:25:09 -05:00 committed by Vincent Prouillet
parent b63c563622
commit 2f1b592ab4

View file

@ -71,7 +71,37 @@ pub fn check_url(url: &str, config: &LinkChecker) -> LinkResult {
Err(e) => LinkResult { code: None, error: Some(e.to_string()) },
}
}
Ok(response) => LinkResult { code: Some(response.status()), error: None },
Ok(response) => {
if response.status().is_success() {
LinkResult { code: Some(response.status()), error: None }
} else {
let error_string = if response.status().is_informational() {
String::from(format!(
"Informational status code ({}) received",
response.status()
))
} else if response.status().is_redirection() {
String::from(format!(
"Redirection status code ({}) received",
response.status()
))
} else if response.status().is_client_error() {
String::from(format!(
"Client error status code ({}) received",
response.status()
))
} else if response.status().is_server_error() {
String::from(format!(
"Server error status code ({}) received",
response.status()
))
} else {
String::from("Non-success status code received")
};
LinkResult { code: None, error: Some(error_string) }
}
}
Err(e) => LinkResult { code: None, error: Some(e.description().to_string()) },
};