@@ -47,37 +47,53 @@ even if the application behaves correctly:
47
47
Error Handlers
48
48
--------------
49
49
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.
55
56
56
57
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.
60
60
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.
63
64
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::
65
69
66
70
from flask import render_template
67
71
68
72
@app.errorhandler(404)
69
73
def page_not_found(e):
74
+ # note that we set the 404 status explicitly
70
75
return render_template('404.html'), 404
71
76
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
+
72
89
An example template might be this:
73
90
74
91
.. sourcecode :: html+jinja
75
92
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