@@ -54,9 +54,11 @@ can be a different error: a handler for internal server errors will be
54
54
passed other exception instances as well if they are uncaught.
55
55
56
56
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.
60
62
61
63
Please note that if you add an error handler for "500 Internal Server
62
64
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::
69
71
def page_not_found(e):
70
72
return render_template('404.html'), 404
71
73
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
+
72
86
An example template might be this:
73
87
74
88
.. sourcecode :: html+jinja
0 commit comments