pub trait ErrorDecoder:
Send
+ Sync
+ 'static {
type Error: Debug + Send + Sync + 'static;
// Required method
fn decode(&self, status: u16, body: &Bytes) -> Option<Self::Error>;
}Expand description
Trait for decoding HTTP error responses into typed errors.
Implement this trait to customize how error responses are handled. The decoder receives the HTTP status code and response body, and can optionally return a decoded error.
§Example
ⓘ
use pincer::ErrorDecoder;
#[derive(Debug, Deserialize)]
struct ApiError {
code: String,
message: String,
}
struct MyErrorDecoder;
impl ErrorDecoder for MyErrorDecoder {
type Error = ApiError;
fn decode(&self, status: u16, body: &bytes::Bytes) -> Option<Self::Error> {
if status >= 400 {
serde_json::from_slice(body).ok()
} else {
None
}
}
}