Skip to content
Myr

Merge pull request #2077 from liebald/patch-1

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Mention existence of register_error_handler in errorpages.rst

authored by David Lord · committed by GitHub · parents 39d5552, 011a4b1 · Browse files · 1 file+35 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+35 added−19 removed

  • other:+35 added−19 removed

1 file+35 added−19 removed

modified:docs/patterns/errorpages.rst+35 added-19 removed
45 unmodified lines
4647Error Handlers48--------------4950An error handler is a function, just like a view function, but it is51called when an error happens and is passed that error. The error is most52likely a :exc:`~werkzeug.exceptions.HTTPException`, but in one case it53can be a different error: a handler for internal server errors will be54passed other exception instances as well if they are uncaught.50An error handler is a function that returns a response when a type of error is51raised, similar to how a view is a function that returns a response when a52request URL is matched. It is passed the instance of the error being handled,53which is most likely a :exc:`~werkzeug.exceptions.HTTPException`. An error54handler for "500 Internal Server Error" will be passed uncaught exceptions in55addition to explicit 500 errors.5657An error handler is registered with the :meth:`~flask.Flask.errorhandler`57decorator and the error code of the exception. Keep in mind that Flask58will *not* set the error code for you, so make sure to also provide the59HTTP status code when returning a response.58decorator or the :meth:`~flask.Flask.register_error_handler` method. A handler59can be registered for a status code, like 404, or for an exception class.6061Please note that if you add an error handler for "500 Internal Server62Error", Flask will not trigger it if it's running in Debug mode.61The status code of the response will not be set to the handler's code. Make62sure to provide the appropriate HTTP status code when returning a response from63a handler.6464Here an example implementation for a "404 Page Not Found" exception::65A handler for "500 Internal Server Error" will not be used when running in66debug mode. Instead, the interactive debugger will be shown.6768Here is an example implementation for a "404 Page Not Found" exception::6970 from flask import render_template7172 @app.errorhandler(404)73 def page_not_found(e):74 # note that we set the 404 status explicitly75 return render_template('404.html'), 4047677When using the :ref:`application factory pattern <app-factories>`::7879 from flask import Flask, render_template8081 def page_not_found(e):82 return render_template('404.html'), 4048384 def create_app(config_filename):85 app = Flask(__name__)86 app.register_error_handler(404, page_not_found)87 return app8889An example template might be this:9091.. sourcecode:: html+jinja9276 {% 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 %}8393 {% 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 %}