pub struct ReqwestError { /* private fields */ }Expand description
The Errors that may occur when processing a Request.
§Examples
extern crate serde;
extern crate reqwest;
use serde::Deserialize;
#[derive(Deserialize)]
struct Simple {
key: String
}
fn run() {
match make_request() {
Err(e) => handler(e),
Ok(_) => return,
}
}
// Response is not a json object conforming to the Simple struct
fn make_request() -> Result<Simple, reqwest::Error> {
reqwest::get("http://httpbin.org/ip")?.json()
}
fn handler(e: reqwest::Error) {
if e.is_http() {
match e.url() {
None => println!("No Url given"),
Some(url) => println!("Problem making request to: {}", url),
}
}
// Inspect the internal error and output it
if e.is_serialization() {
let serde_error = match e.get_ref() {
None => return,
Some(err) => err,
};
println!("problem parsing information {}", serde_error);
}
if e.is_redirect() {
println!("server redirecting too many times or making loop");
}
}Implementations§
Source§impl Error
impl Error
Sourcepub fn url(&self) -> Option<&Url>
pub fn url(&self) -> Option<&Url>
Returns a possible URL related to this error.
§Examples
// displays last stop of a redirect loop
let response = reqwest::get("http://site.with.redirect.loop");
if let Err(e) = response {
if e.is_redirect() {
if let Some(final_stop) = e.url() {
println!("redirect loop at {}", final_stop);
}
}
}Sourcepub fn get_ref(&self) -> Option<&(dyn Error + Sync + Send + 'static)>
pub fn get_ref(&self) -> Option<&(dyn Error + Sync + Send + 'static)>
Returns a reference to the internal error, if available.
The 'static bounds allows using downcast_ref to check the
details of the error.
§Examples
extern crate url;
// retries requests with no host on localhost
let invalid_request = "http://";
let mut response = reqwest::get(invalid_request);
if let Err(e) = response {
match e.get_ref().and_then(|e| e.downcast_ref::<url::ParseError>()) {
Some(&url::ParseError::EmptyHost) => {
let valid_request = format!("{}{}",invalid_request, "localhost");
response = reqwest::get(&valid_request);
},
_ => (),
}
}Sourcepub fn is_timeout(&self) -> bool
pub fn is_timeout(&self) -> bool
Returns true if the error is related to a timeout.
Sourcepub fn is_serialization(&self) -> bool
pub fn is_serialization(&self) -> bool
Returns true if the error is serialization related.
Sourcepub fn is_redirect(&self) -> bool
pub fn is_redirect(&self) -> bool
Returns true if the error is from a RedirectPolicy.
Sourcepub fn is_client_error(&self) -> bool
pub fn is_client_error(&self) -> bool
Returns true if the error is from a request returning a 4xx error.
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Returns true if the error is from a request returning a 5xx error.
Sourcepub fn status(&self) -> Option<StatusCode>
pub fn status(&self) -> Option<StatusCode>
Returns the status code, if the error was generated from a response.
Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§fn cause(&self) -> Option<&dyn Error>
fn cause(&self) -> Option<&dyn Error>
👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Auto Trait Implementations§
impl Freeze for Error
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<E> Fail for E
impl<E> Fail for E
Source§fn cause(&self) -> Option<&(dyn Fail + 'static)>
fn cause(&self) -> Option<&(dyn Fail + 'static)>
Returns a reference to the underlying cause of this failure, if it
is an error that wraps other errors. Read more
Source§fn backtrace(&self) -> Option<&Backtrace>
fn backtrace(&self) -> Option<&Backtrace>
Returns a reference to the
Backtrace carried by this failure, if it
carries one. Read moreSource§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more