Switch to Result-based API. (#132)

Gets rid of synthetic_error, and makes the various send_* methods return `Result<Response, Error>`.
Introduces a new error type "HTTP", which represents an error due to status codes 4xx or 5xx.
The HTTP error type contains a boxed Response, so users can read the actual response if they want.
Adds an `error_for_status` setting to disable the functionality of treating 4xx and 5xx as errors.
Adds .unwrap() to a lot of tests.

Fixes #128.
This commit is contained in:
Jacob Hoffman-Andrews
2020-10-17 00:40:48 -07:00
committed by GitHub
parent 257d4e54dd
commit e36c1c2aa1
19 changed files with 222 additions and 344 deletions

View File

@@ -17,8 +17,8 @@ impl From<io::Error> for Oops {
}
}
impl From<&ureq::Error> for Oops {
fn from(e: &ureq::Error) -> Oops {
impl From<ureq::Error> for Oops {
fn from(e: ureq::Error) -> Oops {
Oops(e.to_string())
}
}
@@ -44,10 +44,7 @@ fn get(agent: &ureq::Agent, url: &String) -> Result<Vec<u8>> {
.get(url)
.timeout_connect(std::time::Duration::from_secs(5))
.timeout(Duration::from_secs(20))
.call();
if let Some(err) = response.synthetic_error() {
return Err(err.into());
}
.call()?;
let mut reader = response.into_reader();
let mut bytes = vec![];
reader.read_to_end(&mut bytes)?;