(Newbie question) Work with Error types


#1

Using reqwest I do

...
            let resp = self.client.head(l).send();
            match resp {
                Ok(s) => {
                    if s.status().is_informational() ||
                        s.status().is_success() ||
                        s.status().is_redirection() {
                            return None;
                    }
                    return Some(s.status());
                }, 
                Err(e) => { println!("{:?}", e); return None }

            }
...

In a certain situation the ‘e’ in Err(e) is:

Error { kind: Io(Custom { kind: WouldBlock, error: StringError(“timed out”) }), url: Some(“http://blablabla”) }

Now I would like to get “timed out” ouf of it.

How do I define a match case?

I tried

Err(Error{kind:IO(Custom{kind:WouldBlock, error:e})})

which might be crap but the compiler shouts at me saying about “Error”

^^^^^ not a struct, variant or union type

It is not clear to me what to import here.


#2

Have you tried https://docs.rs/reqwest/0.8.6/reqwest/struct.Error.html ? You can view it by looking at the docs for https://docs.rs/reqwest/0.8.6/reqwest/struct.Error.html


#3

You won’t be able to destructure in the pattern because the fields are private.

May I ask why you need that string?


#4

I want to give the user of the program a meaningful message which is not just the debug output of a struct.


#5

Have you tried getting the error’s description() or Display output?


#6

I wasn’t aware of description() or Display.

When I use description() this way:

println!("{}", e.description());

I get: other os error
which isn’t really helpful.

I read that description() is somehow depreated. So I tried Display like this:

println!("{}", e);

Output is:

http://blablabla: No route to host (os error 113)

which is much better.

So Display seems to be the way to go. Thanks a lot.