1

With rust 1.49 and reqwest 0.11.0, i would like to retrieve a URL, and react on possible errors (wrong URL, no network, ...).

My (simplifed) base code is (and this is working) :

async fn test_url() -> Result<(), reqwest::Error> {
    let response = reqwest::get("https://aaaaa.bbb").await?;
    info!("Status : {}", response.status());
}

How to handle the possible errors ?

If i try to add something like this

    match response.error_for_status_ref() {
        Ok(_) => {
            info!("Status: {}", response.status());
        },
        Err(err) => {
            error!("Error {}", err);
        },
    }

Then it doesn't work (my code is not executed, like if reqwest is throwing a panic ?).

SRG
  • 1,569
  • 1
  • 17
  • 19
  • 2
    You have the `?` after the await which returns errors immediately from `test_url`, you'll want to `match` on that directly – kmdreko Jan 06 '21 at 00:22

0 Answers0