Merge pull request #2986 from taion/do-not-handle-routing-exception
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Do not handle RoutingExceptions with app error handlers
authored by David Lord · committed by GitHub · parents 38a3918, 662ce21 · Browse files · 3 files·+29 added−3 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+29 added−3 removed
- tests:+6 added−2 removed
- other:+23 added−1 removed
Provenance
- Commit
ded7cbe- Tree
85b74e7- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
38a3918- Parent 2
662ce21
3 files changed+29 added−3 removed
modified:flask/app.py+18 added-1 removed
18 unmodified lines19 20 from werkzeug.datastructures import Headers, ImmutableDict21 from werkzeug.exceptions import BadRequest, BadRequestKeyError, HTTPException, \22 InternalServerError, MethodNotAllowed, default_exceptions23-from werkzeug.routing import BuildError, Map, RequestRedirect, Rule23+from werkzeug.routing import BuildError, Map, RequestRedirect, \24+ RoutingException, Rule25 26 from . import cli, json27 from ._compat import integer_types, reraise, string_types, text_type28 from .config import Config, ConfigAttribute1602 unmodified lines1631 """Handles an HTTP exception. By default this will invoke the1632 registered error handlers and fall back to returning the1633 exception as response.1634 1635+ .. versionchanged:: 1.0.31636+ ``RoutingException``, used internally for actions such as1637+ slash redirects during routing, is not passed to error1638+ handlers.1639+1640+ .. versionchanged:: 1.01641+ Exceptions are looked up by code *and* by MRO, so1642+ ``HTTPExcpetion`` subclasses can be handled with a catch-all1643+ handler for the base ``HTTPException``.1644+1645 .. versionadded:: 0.31646 """1647 # Proxy exceptions don't have error codes. We want to always return1648 # those unchanged as errors1649 if e.code is None:1650 return e1651 1652+ # RoutingExceptions are used internally to trigger routing1653+ # actions, such as slash redirects raising RequestRedirect. They1654+ # are not raised or handled in user code.1655+ if isinstance(e, RoutingException):1656+ return e1657+1658 handler = self._find_error_handler(e)1659 if handler is None:1660 return e1661 return handler(e)673 unmodified linesmodified:tests/test_user_error_handler.py+6 added-2 removed
182 unmodified lines183 @app.route('/forbidden')184 def forbidden():185 raise Forbidden()186 187+ @app.route("/slash/")188+ def slash():189+ return "slash"190+191 app.register_blueprint(bp, url_prefix='/bp')192 193 c = app.test_client()194 assert c.get('/bp/undefined').data == b'bp-default'195 assert c.get('/bp/forbidden').data == b'bp-forbidden'196 assert c.get('/undefined').data == b'default'197 assert c.get('/forbidden').data == b'forbidden'194-195-198+ # Don't handle RequestRedirect raised when adding slash.199+ assert c.get("/slash", follow_redirects=True).data == b"slash"modified:CHANGES.rst+5 added
17 unmodified lines18 - ``flask --version`` outputs Werkzeug's version and simplifies the19 Python version. (`#2825`_)20 - :func:`send_file` handles an ``attachment_filename`` that is a21 native Python 2 string (bytes) with UTF-8 coded bytes. (`#2933`_)22+- A catch-all error handler registered for ``HTTPException`` will not23+ handle ``RoutingExcpetion``, which is used internally during24+ routing. This fixes the unexpected behavior that had been introduced25+ in 1.0. (`#2986`_)26 27 .. _#2766: https://github.com/pallets/flask/issues/276628 .. _#2765: https://github.com/pallets/flask/pull/276529 .. _#2825: https://github.com/pallets/flask/pull/282530 .. _#2933: https://github.com/pallets/flask/issues/293331+.. _#2986: https://github.com/pallets/flask/pull/298632 33 34 Version 1.0.235 -------------896 unmodified lines