pub enum Error {
Show 23 variants
InvalidBaseUrl(ParseError),
Transport {
kind: TransportKind,
message: String,
source: Option<Arc<dyn Error + Send + Sync>>,
},
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),
QuerySerialize(String),
InvalidHeaderName(String),
InvalidHeaderValue(String),
MissingPathParam(String),
SchemaRoute {
method: String,
path: String,
},
SchemaValidation {
phase: &'static str,
message: String,
},
InvalidAuthHeader(String),
NonReplayableBody,
RequestValidation {
message: String,
},
Io(String),
Config(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
on buffered (send / send_json) or streaming responses.
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.
QuerySerialize(String)
Query parameter serialization failed (typed endpoint query).
Returned from EndpointRequestBuilder::query and
EndpointQuery::apply_query when serde serialization fails (since 0.4.0).
InvalidHeaderName(String)
Invalid HTTP header name (RequestBuilder::header).
InvalidHeaderValue(String)
Invalid HTTP header value (RequestBuilder::header).
MissingPathParam(String)
A :param segment in the path template was not supplied.
SchemaRoute
Route not registered in a strict SchemaRegistry (feature schema).
SchemaValidation
schema-validate only.JSON Schema validation failed (feature schema-validate).
InvalidAuthHeader(String)
Invalid Authorization header value.
NonReplayableBody
Request body cannot be replayed for automatic retry (stream or multipart).
RequestValidation
validate only.Request body failed validation before send (feature validate).
Io(String)
I/O error (file writes, etc.).
Config(String)
Internal client configuration error.
Other(String)
Catch-all for rare 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_with_source(
kind: TransportKind,
message: impl Into<String>,
source: impl Error + Send + Sync + 'static,
) -> Self
pub fn transport_with_source( kind: TransportKind, message: impl Into<String>, source: impl Error + Send + Sync + 'static, ) -> Self
Builds a transport error with an underlying source error.
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_source(&self) -> Option<&(dyn Error + Send + Sync)>
pub fn transport_source(&self) -> Option<&(dyn Error + Send + Sync)>
Returns the transport error source when present.
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 query_serialize(message: impl Into<String>) -> Self
pub fn query_serialize(message: impl Into<String>) -> Self
Builds a query serialization error.
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<Error> for DiagnosticError
Available on crate feature miette only.
impl From<Error> for DiagnosticError
miette only.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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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