Implementing error-handling solutions
Chapter 1 showcased the use of the redirect()
method in rendering error pages given a status code, such as the status code 500
. We will now discuss a better way of managing exceptions and status codes, including triggering error pages per status code.
Flask applications must always implement an error-handling mechanism using any of the following strategies:
- Registers a custom error function using the app’s
register_error_handler()
method. - Creates an error handler using the app’s
errorhandler
decorator. - Throws a custom
Exception
class.
Using the register_error_handler method
The declarative way to implement an error handler is to create a custom function and register it to the app
’s register_error_handler()
method. The custom function must have a single local parameter that will accept the injected error message from the platform. It must also return its assigned error page using the make_response...