clean up error handler docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 6032c94 · Browse files · 1 file·+27 added−25 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+27 added−25 removed
- other:+27 added−25 removed
Provenance
- Commit
011a4b1- Tree
7d66bbc- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
6032c94
1 file changed+27 added−25 removed
modified:docs/patterns/errorpages.rst+27 added-25 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 (alternatively, you can use the58-:meth:`~flask.Flask.register_error_handler` function, e.g., when you're59-registering error handlers as part of your Application Factory). Keep in mind60-that Flask will *not* set the error code for you, so make sure to also provide61-the HTTP status code when returning a response.delete_cookie.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 63-Please note that if you add an error handler for "500 Internal Server64-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 66-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 74-And, using an application factory pattern (see :ref:`app-factories`)::77+When using the :ref:`application factory pattern <app-factories>`::78 79 from flask import Flask, render_template77- 80+81 def page_not_found(e):82 return render_template('404.html'), 40480- 83+84 def create_app(config_filename):85 app = Flask(__name__)83- # ...86 app.register_error_handler(404, page_not_found)87+ return app88 89 An example template might be this:90 91 .. sourcecode:: html+jinja92 90- {% extends "layout.html" %}91- {% block title %}Page Not Found{% endblock %}92- {% block body %}93- <h1>Page Not Found</h1>94- <p>What you were looking for is just not there.95- <p><a href="{{ url_for('index') }}">go somewhere nice</a>96- {% endblock %}97-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 %}