Skip to content

Question: how to implement custom error handler for 404 not found #2563

@CSRaghunandan

Description

@CSRaghunandan

I want to send JSON responses for all types of errors.

I tried to do something like for 404 not found errors, but not sure how to proceed further:

pub fn not_found_error_handler<B>(
    mut res: dev::ServiceResponse<B>,
) -> actix_web::Result<middleware::ErrorHandlerResponse<B>> {
    let req = res.request();
    let res = res.map_body(|_, _| {/* something needs to go here to modify response body to include json */});
    Ok(middleware::ErrorHandlerResponse::Response(res))
}

I'm using the latest actix web beta version 18

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions