#[non_exhaustive]pub enum Error {
Protocol(Error),
InvalidStatusCodeWithDetails {
status: StatusCode,
body: String,
headers: HeaderMap,
},
InvalidHeaderValue(InvalidHeaderValue),
NoHeaders,
StreamEnded,
InvalidContentType(HeaderValue),
Instance(Box<dyn Error + Send + Sync + 'static>),
Unbuilt(Box<dyn Error + Send + Sync + 'static>),
}Expand description
Why an HTTP request failed: a protocol or header error, a non-success reply, the transport’s own error, or a client that could not be built.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Protocol(Error)
The request could not be built.
InvalidStatusCodeWithDetails
Failed HTTP response preserving its status, body, and headers.
Transport failures without a response use Self::Instance, a
protocol variant, or Self::Unbuilt when no client could be built.
Fields
status: StatusCodeThe non-success status.
InvalidHeaderValue(InvalidHeaderValue)
A header value with characters a header cannot carry.
NoHeaders
The request builder failed earlier, so its headers cannot be read.
StreamEnded
The response stream ended before it was done.
InvalidContentType(HeaderValue)
The response’s content type was not the one expected.
Instance(Box<dyn Error + Send + Sync + 'static>)
target_family=wasm only.The transport’s own error, without a response.
Unbuilt(Box<dyn Error + Send + Sync + 'static>)
target_family=wasm only.The transport’s client could not be built, so nothing was sent. Sending again fails the same way.
Implementations§
Source§impl Error
impl Error
Sourcepub fn non_success_status(&self) -> Option<StatusCode>
pub fn non_success_status(&self) -> Option<StatusCode>
Preserved HTTP failure status, or None for other error variants.
Sourcepub fn non_success_body(&self) -> Option<&str>
pub fn non_success_body(&self) -> Option<&str>
The response body this error preserved, when it has one. Companion to
Self::non_success_status and Self::non_success_headers.
Sourcepub fn non_success_with_details(
status: StatusCode,
headers: HeaderMap,
body: String,
) -> Self
pub fn non_success_with_details( status: StatusCode, headers: HeaderMap, body: String, ) -> Self
Constructs a failure from its HTTP status, headers, and body without validating that the status is non-successful.
Sourcepub fn non_success_headers(&self) -> Option<&HeaderMap>
pub fn non_success_headers(&self) -> Option<&HeaderMap>
Returns the failed response’s headers, when this error preserved them.
The following example reads the seconds form of Retry-After:
fn retry_after(error: &Error) -> Option<Duration> {
let seconds = error
.non_success_headers()?
.get(http::header::RETRY_AFTER)?
.to_str()
.ok()?
.parse()
.ok()?;
Some(Duration::from_secs(seconds))
}Returns None for a response-less failure: every non-success error
carries its headers.
Source§impl Error
impl Error
Sourcepub fn instance<E: Error + Send + Sync + 'static>(error: E) -> Self
Available on non-target_family=wasm only.
pub fn instance<E: Error + Send + Sync + 'static>(error: E) -> Self
target_family=wasm only.Wrap a transport’s native error as Error::Instance. Transports use
this for response-less failures (connect, decode, timeout); non-success
responses go through Error::non_success_with_details instead so the
status stays inspectable.
Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()