Revert deletions of client_error and friends.

This commit is contained in:
Jacob Hoffman-Andrews
2020-12-02 00:10:36 -08:00
committed by Martin Algesten
parent 64ebd47979
commit 18a9b08973
4 changed files with 28 additions and 3 deletions

View File

@@ -121,8 +121,8 @@ impl Request {
let response = let response =
unit::connect(unit, true, 0, reader, false).map_err(|e| e.url(url.clone()))?; unit::connect(unit, true, 0, reader, false).map_err(|e| e.url(url.clone()))?;
if self.error_on_non_2xx && response.status() >= 400 { if response.error() && self.error_on_non_2xx {
Err(Error::Status(response.status(), response)) Err(ErrorKind::HTTP.new().url(url.clone()).response(response))
} else { } else {
Ok(response) Ok(response)
} }

View File

@@ -145,6 +145,30 @@ impl Response {
.collect() .collect()
} }
/// Whether the response status is: 200 <= status <= 299
pub fn ok(&self) -> bool {
self.status >= 200 && self.status <= 299
}
pub fn redirect(&self) -> bool {
self.status >= 300 && self.status <= 399
}
/// Whether the response status is: 400 <= status <= 499
pub fn client_error(&self) -> bool {
self.status >= 400 && self.status <= 499
}
/// Whether the response status is: 500 <= status <= 599
pub fn server_error(&self) -> bool {
self.status >= 500 && self.status <= 599
}
/// Whether the response status is: 400 <= status <= 599
pub fn error(&self) -> bool {
self.client_error() || self.server_error()
}
/// The content type part of the "Content-Type" header without /// The content type part of the "Content-Type" header without
/// the charset. /// the charset.
/// ///

View File

@@ -175,6 +175,7 @@ pub fn no_status_text() {
test::make_response(200, "", vec![], vec![]) test::make_response(200, "", vec![], vec![])
}); });
let resp = get("test://host/no_status_text").call().unwrap(); let resp = get("test://host/no_status_text").call().unwrap();
assert!(resp.ok());
assert_eq!(resp.status(), 200); assert_eq!(resp.status(), 200);
} }

View File

@@ -231,7 +231,7 @@ pub(crate) fn connect(
save_cookies(&unit, &resp); save_cookies(&unit, &resp);
// handle redirects // handle redirects
if resp.status() >= 300 && resp.status() < 400 && unit.agent.config.redirects > 0 { if resp.redirect() && unit.agent.config.redirects > 0 {
if redirect_count == unit.agent.config.redirects { if redirect_count == unit.agent.config.redirects {
return Err(ErrorKind::TooManyRedirects.new()); return Err(ErrorKind::TooManyRedirects.new());
} }