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
Provenance
- Commit
06f2be3- Tree
244db5b- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
dac45f6- Parent 2
290372f
3 files changed+69 added−2 removed
modified:docs/errorhandling.rst+50 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+You might want to show custom error pages to the user when an error occurs.39+This can be done by registering error handlers.40+41+Error handlers are normal :ref:`views` but instead of being registered for42+routes they are registered for exceptions that are rised while trying to43+do something else.44+45+Registering46+```````````47+48+Register error handlers using :meth:`~flask.Flask.errorhandler` or49+:meth:`~flask.Flask.register_error_handler`::50+51+ @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!')56+57+Those two ways are equivalent, but the first one is more clear and leaves58+you 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 codes61+are interchangable when handed to the registration methods or decorator62+(``BadRequest.code == 400``).63+64+You are however not limited to a :exc:`~werkzeug.exceptions.HTTPException`65+or its code but can register a handler for every exception class you like.66+67+Handling68+````````69+70+Once an exception instance is raised, its class hierarchy is traversed,71+and searched for in the exception classes for which handlers are registered.72+The most specific handler is selected.73+74+E.g. if a instance of :exc:`ConnectionRefusedError` is raised, and a handler75+is registered for :exc:`ConnectionError` and :exc:`ConnectionRefusedError`,76+the more specific :exc:`ConnectionRefusedError` handler is called on the77+exception instance, and its response is shown to the user.78+79 Error Mails80 -----------81 82 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+17 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+ Trying to register a handler on an instance now raises :exc:`ValueError`.48+49 .. _upgrading-to-010:50 51 Version 0.1052 ------------347 unmodified lines