reqwest_middleware/
error.rs

1use reqwest::{StatusCode, Url};
2use thiserror::Error;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Error, Debug)]
7pub enum Error {
8    /// There was an error running some middleware
9    #[error(transparent)]
10    Middleware(#[from] anyhow::Error),
11    /// Error from the underlying reqwest client
12    #[error(transparent)]
13    Reqwest(#[from] reqwest::Error),
14}
15
16impl Error {
17    pub fn middleware<E>(err: E) -> Self
18    where
19        E: 'static + Send + Sync + std::error::Error,
20    {
21        Error::Middleware(err.into())
22    }
23
24    /// Returns a possible URL related to this error.
25    pub fn url(&self) -> Option<&Url> {
26        match self {
27            Error::Middleware(_) => None,
28            Error::Reqwest(e) => e.url(),
29        }
30    }
31
32    /// Returns a mutable reference to the URL related to this error.
33    ///
34    /// This is useful if you need to remove sensitive information from the URL
35    /// (e.g. an API key in the query), but do not want to remove the URL
36    /// entirely.
37    pub fn url_mut(&mut self) -> Option<&mut Url> {
38        match self {
39            Error::Middleware(_) => None,
40            Error::Reqwest(e) => e.url_mut(),
41        }
42    }
43
44    /// Adds a url related to this error (overwriting any existing).
45    pub fn with_url(self, url: Url) -> Self {
46        match self {
47            Error::Middleware(_) => self,
48            Error::Reqwest(e) => e.with_url(url).into(),
49        }
50    }
51
52    /// Strips the related URL from this error (if, for example, it contains
53    /// sensitive information).
54    pub fn without_url(self) -> Self {
55        match self {
56            Error::Middleware(_) => self,
57            Error::Reqwest(e) => e.without_url().into(),
58        }
59    }
60
61    /// Returns true if the error is from any middleware.
62    pub fn is_middleware(&self) -> bool {
63        match self {
64            Error::Middleware(_) => true,
65            Error::Reqwest(_) => false,
66        }
67    }
68
69    /// Returns true if the error is from a type `Builder`.
70    pub fn is_builder(&self) -> bool {
71        match self {
72            Error::Middleware(_) => false,
73            Error::Reqwest(e) => e.is_builder(),
74        }
75    }
76
77    /// Returns true if the error is from a `RedirectPolicy`.
78    pub fn is_redirect(&self) -> bool {
79        match self {
80            Error::Middleware(_) => false,
81            Error::Reqwest(e) => e.is_redirect(),
82        }
83    }
84
85    /// Returns true if the error is from `Response::error_for_status`.
86    pub fn is_status(&self) -> bool {
87        match self {
88            Error::Middleware(_) => false,
89            Error::Reqwest(e) => e.is_status(),
90        }
91    }
92
93    /// Returns true if the error is related to a timeout.
94    pub fn is_timeout(&self) -> bool {
95        match self {
96            Error::Middleware(_) => false,
97            Error::Reqwest(e) => e.is_timeout(),
98        }
99    }
100
101    /// Returns true if the error is related to the request.
102    pub fn is_request(&self) -> bool {
103        match self {
104            Error::Middleware(_) => false,
105            Error::Reqwest(e) => e.is_request(),
106        }
107    }
108
109    #[cfg(not(target_arch = "wasm32"))]
110    /// Returns true if the error is related to connect.
111    pub fn is_connect(&self) -> bool {
112        match self {
113            Error::Middleware(_) => false,
114            Error::Reqwest(e) => e.is_connect(),
115        }
116    }
117
118    /// Returns true if the error is related to the request or response body.
119    pub fn is_body(&self) -> bool {
120        match self {
121            Error::Middleware(_) => false,
122            Error::Reqwest(e) => e.is_body(),
123        }
124    }
125
126    /// Returns true if the error is related to decoding the response's body.
127    pub fn is_decode(&self) -> bool {
128        match self {
129            Error::Middleware(_) => false,
130            Error::Reqwest(e) => e.is_decode(),
131        }
132    }
133
134    /// Returns the status code, if the error was generated from a response.
135    pub fn status(&self) -> Option<StatusCode> {
136        match self {
137            Error::Middleware(_) => None,
138            Error::Reqwest(e) => e.status(),
139        }
140    }
141}