Skip to content
Myr

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

3 files+72 added−2 removed

modified:docs/errorhandling.rst+55 added-2 removed
1.. _application-errors:23Logging Application Errors4==========================3Application Errors4==================56.. versionadded:: 0.378Applications fail, servers fail. Sooner or later you will see an exception
18 unmodified lines
2728But there is more you can do, and we will cover some better setups to deal29with errors.3031.. _error-handlers:3233Error handlers34--------------3536.. versionadded:: 1.03738Some errors are unavoidable because not only you make them. Users might39enter a wrong password or do something different in a way that you just40cannot allow. Or a third party service that your site relies on is41unreachable while an user tries to do something with it.4243In those cases you might want to show error pages to the user in response.44This can be done by registering error handlers.4546Error handlers are normal :ref:`views` but instead of being registered for47routes they are registered exceptions that are rised while trying to do48something else.4950Registering51```````````5253Register error handlers using :meth:`~flask.Flask.errorhandler` or54:meth:`~flask.Flask.register_error_handler`::5556 @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!')6162Those two ways are equivalent, but the first one is more clear and leaves63you 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 codes66are interchangable when handed to the registration methods or decorator67(``BadRequest.code == 400``).6869You are however not limited to a :exc:`~werkzeug.exceptions.HTTPException`70or its code but can register a handler for every exception class you like.7172Handling73````````7475Once an exception instance is raised, its class hierarchy is traversed,76and searched for in the exception classes for which handlers are registered.77The most specific handler is selected.7879E.g. if a instance of :exc:`ConnectionRefusedError` is raised, and a handler80is registered for :exc:`ConnectionError` and :exc:`ConnectionRefusedError`,81the more specific :exc:`ConnectionRefusedError` handler is called on the82exception instance, and its response is shown to the user.8384Error Mails85-----------8687If the application runs in production mode (which it will do on your
267 unmodified lines
modified:docs/quickstart.rst+2 added
699 unmodified lines
700Note the ``404`` after the :func:`~flask.render_template` call. This701tells Flask that the status code of that page should be 404 which means702not found. By default 200 is assumed which translates to: all went well.703704See :ref:`error-handlers` for more details.705706.. _about-responses:707708About Responses709---------------
181 unmodified lines
modified:docs/upgrading.rst+15 added
27 unmodified lines
28applications. Instead the new ``LOGGER_HANDLER_POLICY`` configuration can29be used to disable the default log handlers and custom log handlers can be30set up.3132The behavior of error handlers was changed.33The 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.36Now the inheritance hierarchy takes precedence and handlers for more37specific exception classes are executed instead of more general ones.38See :ref:`error-handlers` for specifics.3940.. note::4142 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.4647.. _upgrading-to-010:4849Version 0.1050------------
347 unmodified lines