Skip to content
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

Merged
merged 22 commits into from Sep 15, 2022

Conversation

junbl
Copy link
Contributor

@junbl junbl commented Jun 15, 2022

PR Type

Feature

PR Checklist

  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • A changelog entry has been made for the appropriate packages.
  • Format code with the latest stable rustfmt.
  • (Team) Label with affected crates and semver status.

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 specified
  • default_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.

@robjtede robjtede added B-semver-minor A-web project: actix-web labels Jun 23, 2022
@robjtede robjtede requested a review from a team June 23, 2022 13:53
@@ -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
Copy link
Member

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.

Copy link
Contributor Author

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!

@robjtede robjtede requested a review from a team June 23, 2022 13:55
@robjtede robjtede added this to the actix-web v4.3 milestone Jun 27, 2022
@junbl
Copy link
Contributor Author

junbl commented Jul 18, 2022

Check failed due to rust-lang/rust#99261 which looks to have been fixed on latest nightly

@robjtede
Copy link
Member

Thanks for the contribution 👍🏻

@robjtede robjtede enabled auto-merge (squash) September 15, 2022 13:00
@robjtede robjtede merged commit bd5c0af into actix:master Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-web project: actix-web B-semver-minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide/document a way to set a blanket/default handler for 4xx/5xx errors
3 participants