pub enum Error {
InvalidBaseUrl(ParseError),
Transport {
kind: TransportKind,
message: String,
},
Http {
status: StatusCode,
status_text: String,
message: String,
body: Option<Bytes>,
},
Deserialize {
status: StatusCode,
message: String,
body: Option<Bytes>,
},
Validation {
status: StatusCode,
message: String,
body: Option<Bytes>,
},
Timeout,
Cancelled,
BodyTooLarge {
limit: u64,
},
MissingBaseUrl,
RetryExhausted {
attempts: u32,
last: Option<Box<Error>>,
},
Hook(String),
Other(String),
}Expand description
Error type for better-fetch operations.
Variants§
InvalidBaseUrl(ParseError)
Base URL parsing failed (ClientBuilder::base_url).
Transport
Underlying transport failure (connection, DNS, body read, etc.).
Fields
kind: TransportKindCoarse category aligned with reqwest’s is_* helpers.
Http
Non-success HTTP response (when using throw mode or send_json).
Fields
status: StatusCodeHTTP status code.
Deserialize
json only.JSON response could not be deserialized (feature json).
Validation
validate only.Response failed garde validation (feature validate).
Timeout
Request exceeded the configured timeout.
Cancelled
Request was cancelled via CancellationToken.
BodyTooLarge
Response body exceeded ClientBuilder::max_response_bytes
or a per-request RequestBuilder::max_response_bytes limit.
MissingBaseUrl
RetryExhausted
Transport retries were exhausted.
Fields
Hook(String)
Returned from on_request or
on_response to abort the pipeline.
Prefer constructing this with Error::hook rather than Error::Other.
Other(String)
Catch-all for configuration or plugin errors.
Implementations§
Source§impl Error
impl Error
Sourcepub fn transport(kind: TransportKind, message: impl Into<String>) -> Self
pub fn transport(kind: TransportKind, message: impl Into<String>) -> Self
Builds a transport error with an explicit TransportKind.
Sourcepub fn transport_message(message: impl Into<String>) -> Self
pub fn transport_message(message: impl Into<String>) -> Self
Builds a transport error with TransportKind::Other.
Sourcepub fn transport_kind(&self) -> Option<TransportKind>
pub fn transport_kind(&self) -> Option<TransportKind>
Returns the transport failure category when this error is Error::Transport.
Sourcepub fn transport_detail(&self) -> Option<&str>
pub fn transport_detail(&self) -> Option<&str>
Returns the transport error detail string when this error is Error::Transport.
Sourcepub fn is_transport(&self) -> bool
pub fn is_transport(&self) -> bool
Returns true when this error is Error::Transport.
Sourcepub fn is_timeout(&self) -> bool
pub fn is_timeout(&self) -> bool
Returns true when this error is Error::Timeout.
Sourcepub fn http(
status: StatusCode,
message: impl Into<String>,
body: Option<Bytes>,
) -> Self
pub fn http( status: StatusCode, message: impl Into<String>, body: Option<Bytes>, ) -> Self
Builds an HTTP error with canonical status text.
Sourcepub fn http_with_status_text(
status: StatusCode,
status_text: impl Into<String>,
message: impl Into<String>,
body: Option<Bytes>,
) -> Self
pub fn http_with_status_text( status: StatusCode, status_text: impl Into<String>, message: impl Into<String>, body: Option<Bytes>, ) -> Self
Builds an HTTP error with explicit status text.
Sourcepub fn status(&self) -> Option<StatusCode>
pub fn status(&self) -> Option<StatusCode>
Returns the HTTP status when this error is response-related.
Sourcepub fn status_text(&self) -> Option<&str>
pub fn status_text(&self) -> Option<&str>
Returns the canonical status text for Error::Http.
Sourcepub fn body(&self) -> Option<&Bytes>
pub fn body(&self) -> Option<&Bytes>
Returns the response body when present on HTTP, deserialize, or validation errors.
Sourcepub fn is_retry_exhausted(&self) -> bool
pub fn is_retry_exhausted(&self) -> bool
Returns true when transport retries were configured but all attempts failed.
Sourcepub fn retry_exhausted_last(&self) -> Option<&Error>
pub fn retry_exhausted_last(&self) -> Option<&Error>
Returns the last error from Error::RetryExhausted when present.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns true when the request was cancelled via CancellationToken.
Sourcepub fn is_body_too_large(&self) -> bool
pub fn is_body_too_large(&self) -> bool
Returns true when the response body exceeded a configured size limit.
Sourcepub fn body_too_large_limit(&self) -> Option<u64>
pub fn body_too_large_limit(&self) -> Option<u64>
Returns the configured byte limit when this error is Error::BodyTooLarge.
Sourcepub fn hook(msg: impl Into<String>) -> Self
pub fn hook(msg: impl Into<String>) -> Self
Builds a hook failure for Hooks::on_request /
Hooks::on_response.
Sourcepub fn is_hook(&self) -> bool
pub fn is_hook(&self) -> bool
Returns true when the error is Error::Hook.
Sourcepub fn api_json<T: DeserializeOwned>(&self) -> Option<T>
Available on crate feature json only.
pub fn api_json<T: DeserializeOwned>(&self) -> Option<T>
json only.Parses the error response body as JSON (for API error payloads).
§Examples
use better_fetch::Error;
use http::StatusCode;
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct ApiError {
message: String,
}
let err = Error::http_with_status_text(
StatusCode::BAD_REQUEST,
"Bad Request",
"bad request",
Some(bytes::Bytes::from_static(br#"{"message":"invalid"}"#)),
);
let api: ApiError = err.api_json().unwrap();
assert_eq!(api.message, "invalid");Sourcepub fn api_json_validated<T>(&self) -> Option<T>
Available on crate feature validate only.
pub fn api_json_validated<T>(&self) -> Option<T>
validate only.Parses and validates the error response body (feature validate).
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()
Source§impl From<ParseError> for Error
impl From<ParseError> for Error
Source§fn from(source: ParseError) -> Self
fn from(source: ParseError) -> Self
Auto Trait Implementations§
impl !Freeze for Error
impl RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnsafeUnpin 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more