Skip to main content

ErrorHandler

Trait ErrorHandler 

Source
pub trait ErrorHandler: Send + Sync {
    // Required method
    fn handle(&self, err: AuthError) -> Response;
}
Expand description

Convert an AuthError into an HTTP Response.

A default implementation is provided that returns an empty 401 for token errors and an empty 500 for internal errors, preserving backward-compatible behaviour.

Register a custom handler once at startup with set_error_handler.

§Example

use chopin_auth::extractor::{ErrorHandler, set_error_handler};
use chopin_auth::jwt::AuthError;
use chopin_core::http::Response;

struct JsonErrors;
impl ErrorHandler for JsonErrors {
    fn handle(&self, err: AuthError) -> Response {
        let body = format!(r#"{{"error":"{err}"}}");
        Response::json(401, &body)
    }
}

set_error_handler(JsonErrors);

Required Methods§

Source

fn handle(&self, err: AuthError) -> Response

Convert err into an HTTP response that will be returned to the client.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§