Skip to content

Commit 7bd54e9

Browse files
authored
Merge pull request pallets#2077 from liebald/patch-1
Mention existence of register_error_handler in errorpages.rst
2 parents 39d5552 + 011a4b1 commit 7bd54e9

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

docs/patterns/errorpages.rst

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,53 @@ even if the application behaves correctly:
4747
Error Handlers
4848
--------------
4949

50-
An error handler is a function, just like a view function, but it is
51-
called when an error happens and is passed that error. The error is most
52-
likely a :exc:`~werkzeug.exceptions.HTTPException`, but in one case it
53-
can be a different error: a handler for internal server errors will be
54-
passed other exception instances as well if they are uncaught.
50+
An error handler is a function that returns a response when a type of error is
51+
raised, similar to how a view is a function that returns a response when a
52+
request URL is matched. It is passed the instance of the error being handled,
53+
which is most likely a :exc:`~werkzeug.exceptions.HTTPException`. An error
54+
handler for "500 Internal Server Error" will be passed uncaught exceptions in
55+
addition to explicit 500 errors.
5556

5657
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.
58+
decorator or the :meth:`~flask.Flask.register_error_handler` method. A handler
59+
can be registered for a status code, like 404, or for an exception class.
6060

61-
Please note that if you add an error handler for "500 Internal Server
62-
Error", Flask will not trigger it if it's running in Debug mode.
61+
The status code of the response will not be set to the handler's code. Make
62+
sure to provide the appropriate HTTP status code when returning a response from
63+
a handler.
6364

64-
Here an example implementation for a "404 Page Not Found" exception::
65+
A handler for "500 Internal Server Error" will not be used when running in
66+
debug mode. Instead, the interactive debugger will be shown.
67+
68+
Here is an example implementation for a "404 Page Not Found" exception::
6569

6670
from flask import render_template
6771

6872
@app.errorhandler(404)
6973
def page_not_found(e):
74+
# note that we set the 404 status explicitly
7075
return render_template('404.html'), 404
7176

77+
When using the :ref:`application factory pattern <app-factories>`::
78+
79+
from flask import Flask, render_template
80+
81+
def page_not_found(e):
82+
return render_template('404.html'), 404
83+
84+
def create_app(config_filename):
85+
app = Flask(__name__)
86+
app.register_error_handler(404, page_not_found)
87+
return app
88+
7289
An example template might be this:
7390

7491
.. sourcecode:: html+jinja
7592

76-
{% extends "layout.html" %}
77-
{% block title %}Page Not Found{% endblock %}
78-
{% block body %}
79-
<h1>Page Not Found</h1>
80-
<p>What you were looking for is just not there.
81-
<p><a href="{{ url_for('index') }}">go somewhere nice</a>
82-
{% endblock %}
83-
93+
{% extends "layout.html" %}
94+
{% block title %}Page Not Found{% endblock %}
95+
{% block body %}
96+
<h1>Page Not Found</h1>
97+
<p>What you were looking for is just not there.
98+
<p><a href="{{ url_for('index') }}">go somewhere nice</a>
99+
{% endblock %}

0 commit comments

Comments
 (0)