pub enum Error {
InvalidBaseUrl(ParseError),
Transport(String),
Http {
status: StatusCode,
status_text: String,
message: String,
body: Option<Bytes>,
},
Deserialize {
status: StatusCode,
message: String,
body: Option<Bytes>,
},
Timeout,
Cancelled,
MissingBaseUrl,
RetryExhausted {
attempts: u32,
last: Option<String>,
},
Hook(String),
Other(String),
}Expand description
Error type for better-fetch operations.
Variants§
InvalidBaseUrl(ParseError)
Base URL parsing failed (ClientBuilder::base_url).
Transport(String)
Underlying transport failure (connection, DNS, etc.).
Http
Non-success HTTP response (when using throw mode or send_json).
Fields
status: StatusCodeHTTP status code.
Deserialize
JSON response could not be deserialized (feature json).
Timeout
Request exceeded the configured timeout.
Cancelled
Request was cancelled via CancellationToken.
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 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 is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns true when the request was cancelled via CancellationToken.
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>
pub fn api_json<T: DeserializeOwned>(&self) -> Option<T>
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");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()