actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

Add ability to set default error handlers to the `ErrorHandler` middleware

Open junbl opened this issue 3 years ago • 1 comments

PR Type

Feature

PR Checklist

  • [x] Tests for the changes have been added / updated.
  • [x] Documentation comments have been added / updated.
  • [x] A changelog entry has been made for the appropriate packages.
  • [x] Format code with the latest stable rustfmt.
  • [x] (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.

junbl avatar Jun 15 '22 20:06 junbl

Check failed due to https://github.com/rust-lang/rust/issues/99261 which looks to have been fixed on latest nightly

junbl avatar Jul 18 '22 16:07 junbl

Thanks for the contribution 👍🏻

robjtede avatar Sep 15 '22 12:09 robjtede