Skip to content
Myr

Merge pull request #1429 from flying-sheep/errorhandler-rework-docs

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

Added docs for the error handler rework

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents dac45f6, 290372f · Browse files · 3 files+69 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+69 added−2 removed

  • other:+69 added−2 removed

3 files+69 added−2 removed

modified:docs/errorhandling.rst+50 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.03738You might want to show custom error pages to the user when an error occurs.39This can be done by registering error handlers.4041Error handlers are normal :ref:`views` but instead of being registered for42routes they are registered for exceptions that are rised while trying to43do something else.4445Registering46```````````4748Register error handlers using :meth:`~flask.Flask.errorhandler` or49:meth:`~flask.Flask.register_error_handler`::5051 @app.errorhandler(werkzeug.exceptions.BadRequest)52 def handle_bad_request(e):53 return 'bad request!'54 55 app.register_error_handler(400, lambda e: 'bad request!')5657Those two ways are equivalent, but the first one is more clear and leaves58you with a function to call on your whim (and in tests). Note that59:exc:`werkzeug.exceptions.HTTPException` subclasses like60:exc:`~werkzeug.exceptions.BadRequest` from the example and their HTTP codes61are interchangable when handed to the registration methods or decorator62(``BadRequest.code == 400``).6364You are however not limited to a :exc:`~werkzeug.exceptions.HTTPException`65or its code but can register a handler for every exception class you like.6667Handling68````````6970Once an exception instance is raised, its class hierarchy is traversed,71and searched for in the exception classes for which handlers are registered.72The most specific handler is selected.7374E.g. if a instance of :exc:`ConnectionRefusedError` is raised, and a handler75is registered for :exc:`ConnectionError` and :exc:`ConnectionRefusedError`,76the more specific :exc:`ConnectionRefusedError` handler is called on the77exception instance, and its response is shown to the user.7879Error Mails80-----------8182If 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+17 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.46 47 Trying to register a handler on an instance now raises :exc:`ValueError`.4849.. _upgrading-to-010:5051Version 0.1052------------
347 unmodified lines