Added docs for the error handler rework
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Phil Schaf · committed by Phil Schaf · parent dac45f6 · Browse files · 3 files·+72 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+72 added−2 removed
- other:+72 added−2 removed
Provenance
- Commit
9d3c4bb- Tree
c39c3f3- Author
Phil Schaf <flying-sheep@web.de>- Committer
Phil Schaf <flying-sheep@web.de>- Parent
dac45f6
3 files changed+72 added−2 removed
modified:docs/errorhandling.rst+55 added-2 removed
1 .. _application-errors:2 3-Logging Application Errors4-==========================3+Application Errors4+==================5 6 .. versionadded:: 0.37 8 Applications fail, servers fail. Sooner or later you will see an exception18 unmodified lines27 28 But there is more you can do, and we will cover some better setups to deal29 with errors.30 31+.. _error-handlers:32+33+Error handlers34+--------------35+36+.. versionadded:: 1.037+38+Some errors are unavoidable because not only you make them. Users might39+enter a wrong password or do something different in a way that you just40+cannot allow. Or a third party service that your site relies on is41+unreachable while an user tries to do something with it.42+43+In those cases you might want to show error pages to the user in response.44+This can be done by registering error handlers.45+46+Error handlers are normal :ref:`views` but instead of being registered for47+routes they are registered exceptions that are rised while trying to do48+something else.49+50+Registering51+```````````52+53+Register error handlers using :meth:`~flask.Flask.errorhandler` or54+:meth:`~flask.Flask.register_error_handler`::55+56+ @app.errorhandler(werkzeug.exceptions.BadRequest)57+ def handle_bad_request(e):58+ return 'bad request!'59+ 60+ app.register_error_handler(400, lambda e: 'bad request!')61+62+Those two ways are equivalent, but the first one is more clear and leaves63+you with a function to call on your whim (and in tests). Note that64+:exc:`werkzeug.exceptions.HTTPException` subclasses like65+:exc:`~werkzeug.exceptions.BadRequest` from the example and their HTTP codes66+are interchangable when handed to the registration methods or decorator67+(``BadRequest.code == 400``).68+69+You are however not limited to a :exc:`~werkzeug.exceptions.HTTPException`70+or its code but can register a handler for every exception class you like.71+72+Handling73+````````74+75+Once an exception instance is raised, its class hierarchy is traversed,76+and searched for in the exception classes for which handlers are registered.77+The most specific handler is selected.78+79+E.g. if a instance of :exc:`ConnectionRefusedError` is raised, and a handler80+is registered for :exc:`ConnectionError` and :exc:`ConnectionRefusedError`,81+the more specific :exc:`ConnectionRefusedError` handler is called on the82+exception instance, and its response is shown to the user.83+84 Error Mails85 -----------86 87 If the application runs in production mode (which it will do on your267 unmodified linesmodified:docs/quickstart.rst+2 added
699 unmodified lines700 Note the ``404`` after the :func:`~flask.render_template` call. This701 tells Flask that the status code of that page should be 404 which means702 not found. By default 200 is assumed which translates to: all went well.703 704+See :ref:`error-handlers` for more details.705+706 .. _about-responses:707 708 About Responses709 ---------------181 unmodified linesmodified:docs/upgrading.rst+15 added
27 unmodified lines28 applications. Instead the new ``LOGGER_HANDLER_POLICY`` configuration can29 be used to disable the default log handlers and custom log handlers can be30 set up.31 32+The behavior of error handlers was changed.33+The precedence of handlers used to be based on the decoration/call order of34+:meth:`~flask.Flask.errorhandler` and35+:meth:`~flask.Flask.register_error_handler`, respectively.36+Now the inheritance hierarchy takes precedence and handlers for more37+specific exception classes are executed instead of more general ones.38+See :ref:`error-handlers` for specifics.39+40+.. note::41+42+ There used to be a logic error allowing you to register handlers43+ only for exception *instances*. This was unintended and plain wrong,44+ and therefore was replaced with the intended behavior of registering45+ handlers only using exception classes and HTTP error codes.46+47 .. _upgrading-to-010:48 49 Version 0.1050 ------------347 unmodified lines