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

How to handle a none exist url #311

Closed Decodetalkers closed 3 years ago

Decodetalkers commented 3 years ago

image Like this . I have read the doc, but I still cannot handle it..

fiag commented 3 years ago
let mut res = surf::get(url).await?;
if res.status() != surf::StatusCode::Ok {
    println!("Failed to download url");
}
Decodetalkers commented 3 years ago
let mut res = surf::get(url).await?;
if res.status() != surf::StatusCode::Ok {
    println!("Failed to download url");
}

then how to return the surf::error, now my function return a surf::Result.

Thanks very much

Decodetalkers commented 3 years ago
let mut res = surf::get(url).await?;
if res.status() != surf::StatusCode::Ok {
    println!("Failed to download url");
}

I tried, but it not help.

Fishrock123 commented 3 years ago

Please read https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html

Decodetalkers commented 3 years ago
let mut res = surf::get(url).await?;
if res.status() != surf::StatusCode::Ok {
    println!("Failed to download url");
}

Its my code image

Then it panic at "await?", before handle the url image

jbr commented 3 years ago

sss is not a valid url. Surf requires a string that can be parsed into a Url

Decodetalkers commented 3 years ago

sss is not a valid url. Surf requires a string that can be parsed into a Url

This is what I need , I need to handle this error...,when people input a url or not url string, it will pop an error window

jbr commented 3 years ago

ahh. see the example at Request::new:

use surf::http::{Url, Method};

let url = Url::parse("https://httpbin.org/get")?;
let req = surf::Request::new(Method::Get, url);
Decodetalkers commented 3 years ago

ahh. see the example at Request::new:

use surf::http::{Url, Method};

let url = Url::parse("https://httpbin.org/get")?;
let req = surf::Request::new(Method::Get, url);

It works, thanks