⚠️ Warning: This crate is only published to provide documentation on docs.rs. It is not intended to be used viacargo add. Please copy the code manually into your project.
Note: This is not a library — just a code snippet you can copy-paste into your project. Why? Because Rust treats each crate version as a unique type. If this helper were a real library and depended on
reqwest, it would only work when your project uses exactly the same version ofreqwest. Supporting every version would require releasing multiple versions of this crate — which is overkill for a tiny helper like this. So instead, it’s just a clean, dependency-free snippet you can drop into your own codebase.
Save the following code as reqw.rs somewhere in your project (e.g., in src/utils/reqw.rs):
pub enum Error {
Http(reqwest::Response),
Transport(reqwest::Error),
}
/// This function re-estimates response of reqwest
///
/// - Returns `Ok(reqwest::Response)` if status code is 200-299
/// - Returns `Err(Error::Http)` if the status code is not 2xx
/// - Returns `Err(Error::Transport)` if there was a transport error
pub fn est(result: Result<reqwest::Response, reqwest::Error>) -> Result<reqwest::Response, Error> {
match result {
Ok(r) if r.status().is_success() => Ok(r),
Ok(r) => Err(Error::Http(r)),
Err(e) => Err(Error::Transport(e)),
}
}Then, use it like this:
mod utils;
use utils::reqw;
#[tokio::main]
async fn main() {
match reqw::est(reqwest::get("https://example.com").await) {
Ok(res) => println!("✅ Success! Status: {}", res.status()),
Err(reqw::Error::Http(res)) => eprintln!("❌ HTTP error: {}", res.status()),
Err(reqw::Error::Transport(err)) => eprintln!("💥 Transport error: {}", err),
}
}Reqw::est() stands for "estimate" — it redefines how to interpret reqwest's Result<Response, Error>
by re-estimating the outcome of an HTTP request into a simpler, cleaner success/error model that better fits real-world application logic.
In reqwest, the result of an HTTP request looks like this:
Result<reqwest::Response, reqwest::Error>This has three possible real-world meanings:
- ✅ Ok(res) where res.status().is_success() (e.g., 200 OK)
- ✅ Ok(res) where res.status() is an error (e.g., 404, 500)
- ❌ Err(e) for transport-level failures (e.g., timeout, DNS failure)
By default, reqwest treats all Ok(Response) as "success", even if the server returned a 500 Internal Server Error — because the HTTP protocol worked.
Reqw::est() reinterprets that like this:
Result<reqwest::Response, reqw::Error>With:
- ✅ Ok(res) → only if the response is a 2xx success
- ❌ Err(reqw::Error::Http(reqwest::Response)) → if response is non-2xx
- ❌ Err(reqw::Error::Transport(reqwest::Error)) → if the request itself failed (network error, timeout, etc.)
This way, you can treat HTTP error responses in the same way as transport errors — making your control flow simpler and more idiomatic in Rust.