ReqwestError

Struct ReqwestError 

Source
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

Source

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);
        }
    }
}
Source

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);
        },
        _ => (),
    }
}
Source

pub fn is_http(&self) -> bool

Returns true if the error is related to HTTP.

Source

pub fn is_timeout(&self) -> bool

Returns true if the error is related to a timeout.

Source

pub fn is_serialization(&self) -> bool

Returns true if the error is serialization related.

Source

pub fn is_redirect(&self) -> bool

Returns true if the error is from a RedirectPolicy.

Source

pub fn is_client_error(&self) -> bool

Returns true if the error is from a request returning a 4xx error.

Source

pub fn is_server_error(&self) -> bool

Returns true if the error is from a request returning a 5xx error.

Source

pub fn status(&self) -> Option<StatusCode>

Returns the status code, if the error was generated from a response.

Trait Implementations§

Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Error for Error

Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsFail for T
where T: Fail,

Source§

fn as_fail(&self) -> &(dyn Fail + 'static)

Converts a reference to Self into a dynamic trait object of Fail.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<E> Fail for E
where E: Error + Send + Sync + 'static,

Source§

fn name(&self) -> Option<&str>

Returns the “name” of the error. Read more
Source§

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>

Returns a reference to the Backtrace carried by this failure, if it carries one. Read more
Source§

fn context<D>(self, context: D) -> Context<D>
where D: Display + Send + Sync + 'static, Self: Sized,

Provides context for this failure. Read more
Source§

fn compat(self) -> Compat<Self>
where Self: Sized,

Wraps this failure in a compatibility wrapper that implements std::error::Error. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Err = <U as TryFrom<T>>::Err

Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Err>