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
Provenance
- Commit
7bd54e9- Tree
7d66bbc- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
39d5552- Parent 2
011a4b1
1 file changed+35 added−19 removed
modified:docs/patterns/errorpages.rst+35 added-19 removed
45 unmodified lines46 47 Error Handlers48 --------------49 50-An error handler is a function, just like a view function, but it is51-called when an error happens and is passed that error. The error is most52-likely a :exc:`~werkzeug.exceptions.HTTPException`, but in one case it53-can be a different error: a handler for internal server errors will be54-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 is51+raised, similar to how a view is a function that returns a response when a52+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 error54+handler for "500 Internal Server Error" will be passed uncaught exceptions in55+addition to explicit 500 errors.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 Flask58-will *not* set the error code for you, so make sure to also provide the59-HTTP status code when returning a response.58+decorator or the :meth:`~flask.Flask.register_error_handler` method. A handler59+can be registered for a status code, like 404, or for an exception class.60 61-Please note that if you add an error handler for "500 Internal Server62-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. Make62+sure to provide the appropriate HTTP status code when returning a response from63+a handler.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 in66+debug mode. Instead, the interactive debugger will be shown.67 68+Here is an example implementation for a "404 Page Not Found" exception::69+70 from flask import render_template71 72 @app.errorhandler(404)73 def page_not_found(e):74+ # note that we set the 404 status explicitly75 return render_template('404.html'), 40476 77+When using the :ref:`application factory pattern <app-factories>`::78+79+ from flask import Flask, render_template80+81+ def page_not_found(e):82+ return render_template('404.html'), 40483+84+ def create_app(config_filename):85+ app = Flask(__name__)86+ app.register_error_handler(404, page_not_found)87+ return app88+89 An example template might be this:90 91 .. sourcecode:: html+jinja92 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 %}