Getting error source #378
-
I want to get to the source of an error. When I debug-print the error, I can see something called "source", and as I read isahc's documentation, isahc's Error shoud have a source method. However, I don't understand how to get to source. Here's some code demonstrating what I'd like to to:
When the code runs, it looks like this:
If I uncomment the code where I try to get to the source, I get the following error:
What am I misunderstanding? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
You need to import the standard |
Beta Was this translation helpful? Give feedback.
-
OK, thanks. What I really need is the "code" field from the error. I don't understand how to pull that from &dyn std::error::Error. This is my code now:
|
Beta Was this translation helpful? Give feedback.
-
The If you just want to know what kind of error it is, then I recommend using the if let Err(e) = response_res {
match e.kind() {
ErrorKind::Timeout => { /* timeout expired */ }
_ => { /* some other error */ }
}
} Or if you only care about checking for timeouts, if let Err(e) = response_res {
if e.is_timeout() {
/* do something about timeout expired */
} else {
/* some other error */
}
} There are a lot of variants in However, if you really actually need to get the underlying error code (which isn't always as meaningful as you think): In this case the if let Err(e) = response_res {
// Check if the error has a source
if let Some(src) = e.source() {
// Check if the source is an instance of `curl::Error`
if let Some(curl_error) = src.downcast_ref::<curl::Error>() {
// Now we have a `&curl::Error` and can do things with it.
let code = curl_error.code();
// Do something with the code?
}
}
} Again, unless you specifically need a |
Beta Was this translation helpful? Give feedback.
The
source
of anisahc::error::Error
is an underlying error (implementingstd::error::Error
) emitted at a lower level that may have caused the higher-level Isahc error to be returned. We don't guarantee that thesource
is present, and if it is present, we don't guarantee that it will be of a particular type, since that would make it difficult for us to change the implementation details of Isahc. Keep this in mind when trying to introspect errors.If you just want to know what kind of error it is, then I recommend using the
isahc::error::Error::kind
method, which returns potentially one of the enum variants inErrorKind
that broadly classify what failed. For example, if you just want to kn…