From 6032c94aeb090c82ac99b9f886341f61c73b65d0 Mon Sep 17 00:00:00 2001 From: Benjamin Liebald Date: Wed, 2 Nov 2016 13:36:54 -0700 Subject: [PATCH] Mention existence of register_error_handler in errorpages.rst See https://github.com/pallets/flask/issues/1837 for context. --- docs/patterns/errorpages.rst | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/patterns/errorpages.rst b/docs/patterns/errorpages.rst index fccd4a6f..e7ec6b65 100644 --- a/docs/patterns/errorpages.rst +++ b/docs/patterns/errorpages.rst @@ -54,9 +54,11 @@ can be a different error: a handler for internal server errors will be passed other exception instances as well if they are uncaught. An error handler is registered with the :meth:`~flask.Flask.errorhandler` -decorator and the error code of the exception. Keep in mind that Flask -will *not* set the error code for you, so make sure to also provide the -HTTP status code when returning a response. +decorator and the error code of the exception (alternatively, you can use the +:meth:`~flask.Flask.register_error_handler` function, e.g., when you're +registering error handlers as part of your Application Factory). Keep in mind +that Flask will *not* set the error code for you, so make sure to also provide +the HTTP status code when returning a response.delete_cookie. Please note that if you add an error handler for "500 Internal Server Error", Flask will not trigger it if it's running in Debug mode. @@ -69,6 +71,18 @@ Here an example implementation for a "404 Page Not Found" exception:: def page_not_found(e): return render_template('404.html'), 404 +And, using an application factory pattern (see :ref:`app-factories`):: + + from flask import Flask, render_template + + def page_not_found(e): + return render_template('404.html'), 404 + + def create_app(config_filename): + app = Flask(__name__) + # ... + app.register_error_handler(404, page_not_found) + An example template might be this: .. sourcecode:: html+jinja