Skip to content

Commit 6032c94

Browse files
liebalddavidism
authored andcommitted
Mention existence of register_error_handler in errorpages.rst
See pallets#1837 for context.
1 parent 39d5552 commit 6032c94

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

docs/patterns/errorpages.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ can be a different error: a handler for internal server errors will be
5454
passed other exception instances as well if they are uncaught.
5555

5656
An error handler is registered with the :meth:`~flask.Flask.errorhandler`
57-
decorator and the error code of the exception. Keep in mind that Flask
58-
will *not* set the error code for you, so make sure to also provide the
59-
HTTP status code when returning a response.
57+
decorator and the error code of the exception (alternatively, you can use the
58+
:meth:`~flask.Flask.register_error_handler` function, e.g., when you're
59+
registering error handlers as part of your Application Factory). Keep in mind
60+
that Flask will *not* set the error code for you, so make sure to also provide
61+
the HTTP status code when returning a response.delete_cookie.
6062

6163
Please note that if you add an error handler for "500 Internal Server
6264
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::
6971
def page_not_found(e):
7072
return render_template('404.html'), 404
7173

74+
And, using an application factory pattern (see :ref:`app-factories`)::
75+
76+
from flask import Flask, render_template
77+
78+
def page_not_found(e):
79+
return render_template('404.html'), 404
80+
81+
def create_app(config_filename):
82+
app = Flask(__name__)
83+
# ...
84+
app.register_error_handler(404, page_not_found)
85+
7286
An example template might be this:
7387

7488
.. sourcecode:: html+jinja

0 commit comments

Comments
 (0)