pub enum Error {
Http {
status: u16,
message: String,
body: Option<Bytes>,
},
Connection(String),
Tls(String),
Timeout,
InvalidRequest(String),
JsonSerialization(Error),
JsonDeserialization {
path: String,
message: String,
},
FormSerialization(Error),
QuerySerialization(Error),
InvalidUrl(ParseError),
TooManyRedirects {
count: usize,
max: usize,
},
InvalidRedirect(String),
}Expand description
Main error type for pincer operations.
Variants§
Http
HTTP-level errors (non-2xx status codes).
Fields
Connection(String)
Network/connection errors.
Tls(String)
TLS/SSL errors.
Timeout
Request timeout.
InvalidRequest(String)
Invalid request configuration.
JsonSerialization(Error)
JSON serialization error.
JsonDeserialization
JSON deserialization error with path context.
Fields
FormSerialization(Error)
Form URL-encoded serialization error.
QuerySerialization(Error)
Query string serialization error.
InvalidUrl(ParseError)
URL parsing error.
TooManyRedirects
Too many redirects.
InvalidRedirect(String)
Invalid redirect response.
Implementations§
Source§impl Error
impl Error
Sourcepub fn http(status: u16, message: impl Into<String>) -> Self
pub fn http(status: u16, message: impl Into<String>) -> Self
Create an HTTP error from status code and message.
Sourcepub fn http_with_body(
status: u16,
message: impl Into<String>,
body: Bytes,
) -> Self
pub fn http_with_body( status: u16, message: impl Into<String>, body: Bytes, ) -> Self
Create an HTTP error with body.
Sourcepub fn connection(message: impl Into<String>) -> Self
pub fn connection(message: impl Into<String>) -> Self
Create a connection error.
Sourcepub fn invalid_request(message: impl Into<String>) -> Self
pub fn invalid_request(message: impl Into<String>) -> Self
Create an invalid request error.
Sourcepub fn json_deserialization(
path: impl Into<String>,
message: impl Into<String>,
) -> Self
pub fn json_deserialization( path: impl Into<String>, message: impl Into<String>, ) -> Self
Create a JSON deserialization error with path context.
Sourcepub const fn is_timeout(&self) -> bool
pub const fn is_timeout(&self) -> bool
Returns true if this is a timeout error.
Sourcepub const fn is_connection(&self) -> bool
pub const fn is_connection(&self) -> bool
Returns true if this is a connection error.
Sourcepub const fn status(&self) -> Option<u16>
pub const fn status(&self) -> Option<u16>
Returns the HTTP status code if this is an HTTP error.
Sourcepub fn is_client_error(&self) -> bool
pub fn is_client_error(&self) -> bool
Returns true if this is a client error (4xx).
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Returns true if this is a server error (5xx).
Sourcepub fn is_not_found(&self) -> bool
pub fn is_not_found(&self) -> bool
Returns true if this is a 404 Not Found error.
Sourcepub fn body(&self) -> Option<&Bytes>
pub fn body(&self) -> Option<&Bytes>
Returns the response body if this is an HTTP error with a body.
Sourcepub fn decode_body<T: DeserializeOwned>(&self) -> Option<Result<T>>
pub fn decode_body<T: DeserializeOwned>(&self) -> Option<Result<T>>
Try to decode the HTTP error body as JSON.
Returns Some(Ok(value)) if the error has a body and it deserializes successfully,
Some(Err(error)) if the body exists but deserialization fails,
or None if there is no body or this is not an HTTP error.
§Example
#[derive(Debug, Deserialize)]
struct ApiError {
code: String,
message: String,
}
match client.get_user(123).await {
Ok(user) => println!("User: {:?}", user),
Err(e) => {
if let Some(Ok(api_error)) = e.decode_body::<ApiError>() {
println!("API error: {} - {}", api_error.code, api_error.message);
} else {
println!("Error: {}", e);
}
}
}