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:
committed by
GitHub
parent
257d4e54dd
commit
e36c1c2aa1
@@ -17,15 +17,12 @@ let resp = ureq::post("https://myapi.example.com/ingest")
|
||||
.send_json(serde_json::json!({
|
||||
"name": "martin",
|
||||
"rust": true
|
||||
}));
|
||||
}))?;
|
||||
|
||||
// .ok() tells if response is 200-299.
|
||||
if resp.ok() {
|
||||
println!("success: {}", resp.into_string()?);
|
||||
} else {
|
||||
// This can include errors like failure to parse URL or
|
||||
// connect timeout. They are treated as synthetic
|
||||
// HTTP-level error statuses.
|
||||
println!("error {}: {}", resp.status(), resp.into_string()?);
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user