Skip to main content

input_error_handler

Function input_error_handler 

Source
pub fn input_error_handler<E, R>(
    context: InputErrorContext,
) -> impl Fn(E, &R) -> Error + Send + Sync + 'static
where E: InputContext<Output = Error>,
Expand description

Generic error handler for input validation errors.

This handler accepts any type of error that implements InputContext (e.g. can be turned into an Input error), then turns that into an actix_web::error::Error. This is possible because we implemented ResponseError for our Error type - thus, actix_web will use our implementation to generate an appropriate HTTP response for the type of error encountered. This makes it possible to handle pre-request handler errors (like for example DeserializeErrorss) using the same error handling code as in-request handler errors.

§Examples

use actix_web_validator::{JsonConfig, PathConfig, QueryConfig};
use pokedex_rs::api::errors::input_error_handler;
use pokedex_rs::error::InputErrorContext;

let json_config =
    JsonConfig::default().error_handler(input_error_handler(InputErrorContext::Json));
let path_config =
    PathConfig::default().error_handler(input_error_handler(InputErrorContext::Path));
let query_config =
    QueryConfig::default().error_handler(input_error_handler(InputErrorContext::Query));