-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to set default error handlers to the ErrorHandler
middleware
#2784
Conversation
@@ -53,7 +55,38 @@ type ErrorHandler<B> = dyn Fn(ServiceResponse<B>) -> Result<ErrorHandlerResponse | |||
/// .wrap(ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_header)) | |||
/// .service(web::resource("/").route(web::get().to(HttpResponse::InternalServerError))); | |||
/// ``` | |||
/// ## Registering default handler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explain the precedence of overlapping handlers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hope the new docs are clearer. lmk if anything still doesn't make sense!
… into default_error_handler
Check failed due to rust-lang/rust#99261 which looks to have been fixed on latest nightly |
Thanks for the contribution 👍🏻 |
PR Type
Feature
PR Checklist
Overview
Currently, each error handler has to be set separately for each status code. It's cumbersome to have to add a line for each status code, and ripe for errors if some codes are missed.
This PR allows the user to set a default handler either for all errors, or separate defaults for client and server errors, while still allowing other handlers to be tied to specific status codes. It defines the following new methods on
middleware::ErrorHandlers
:default_handler
: sets a default handler that will be used if the given status code doesn't have another handler specifieddefault_handler_client
: sets a default handler only for client errors (400-499)default_handler_server
: sets a default handler only for server errors (500-599)This PR contains no breaking changes; other than the additions above, the only changes are to private fields/types.
Closes #2667.