feat: Introduce Response::error_for_status method

This method makes it possible to let users map responses with
unexpected HTTP statuses to custom errors while staying inside a chain
of results.
This commit is contained in:
Vincent Ambo 2019-02-26 22:21:43 +01:00
parent 481825672e
commit e4e931661b
2 changed files with 27 additions and 1 deletions

View file

@ -115,3 +115,15 @@ fn test_basic_auth() {
assert!(response.is_success(), "authorized request should succeed");
}
// Tests for various other features.
#[test]
fn test_error_for_status() {
let response = Request::new(Method::Get, "https://httpbin.org/patch")
.send().expect("failed to send request")
.error_for_status(|resp| format!("Response error code: {}", resp.status));
assert_eq!(Err("Response error code: 405".into()), response,
"returned error should be converted into Result::Err");
}