http-rs / surf

Fast and friendly HTTP client framework for async Rust
https://docs.rs/surf
Apache License 2.0
1.45k stars 119 forks source link

Some errors are needlessly verbose #336

Closed heroin-moose closed 2 years ago

heroin-moose commented 2 years ago

Hi,

I've stumbled upon a fact that some errors (like connection one for example) are not particularly helpful:

$ cat src/main.rs
use surf::Config;
use surf::Client;
use surf::Result;
use surf::Url;

#[async_std::main]
async fn main() -> Result<()> {
    let url = Url::parse("http://127.0.0.1:8080")?;
    let http: Client = Config::new().set_base_url(url).try_into()?;
    if let Err(e) = http.post("/endpoint").body_json(&String::from("{}"))?.await {
        eprintln!("test-prg: cannot make POST request to http://127.0.0.1:8080/endpoint: {}", e);
    }
    Ok(())
}
$ cargo build --bin test-prg
$ ./target/debug/test-prg
test-prg: cannot make POST request to http://127.0.0.1:8080/endpoint: An error occured while creating a new object: Connection refused (os error 111)

I think it would be better either to report only the status code so the user could add the required context:

Connection refused (os error 111)

or to report something like

Cannot connect to 127.0.0.1 port 8080: Connection refused (os error 111)
heroin-moose commented 2 years ago

Hm, this error seems to be coming from deadpool that simply uses String to store context.