ErrorDecoder

Trait ErrorDecoder 

Source
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
        }
    }
}

Required Associated Types§

Source

type Error: Debug + Send + Sync + 'static

The decoded error type.

Required Methods§

Source

fn decode(&self, status: u16, body: &Bytes) -> Option<Self::Error>

Decode an HTTP error response into a typed error.

Returns Some(error) if the response should be decoded as a custom error, or None to fall back to the default Error::Http handling.

Implementors§