Skip to content

Commit

Permalink
Merge pull request #2986 from taion/do-not-handle-routing-exception
Browse files Browse the repository at this point in the history
Do not handle RoutingExceptions with app error handlers
  • Loading branch information
davidism authored Jan 7, 2019
2 parents 38a3918 + 662ce21 commit ded7cbe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ Unreleased
Python version. (`#2825`_)
- :func:`send_file` handles an ``attachment_filename`` that is a
native Python 2 string (bytes) with UTF-8 coded bytes. (`#2933`_)
- A catch-all error handler registered for ``HTTPException`` will not
handle ``RoutingExcpetion``, which is used internally during
routing. This fixes the unexpected behavior that had been introduced
in 1.0. (`#2986`_)

.. _#2766: https://github.com/pallets/flask/issues/2766
.. _#2765: https://github.com/pallets/flask/pull/2765
.. _#2825: https://github.com/pallets/flask/pull/2825
.. _#2933: https://github.com/pallets/flask/issues/2933
.. _#2986: https://github.com/pallets/flask/pull/2986


Version 1.0.2
Expand Down
19 changes: 18 additions & 1 deletion flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from werkzeug.datastructures import Headers, ImmutableDict
from werkzeug.exceptions import BadRequest, BadRequestKeyError, HTTPException, \
InternalServerError, MethodNotAllowed, default_exceptions
from werkzeug.routing import BuildError, Map, RequestRedirect, Rule
from werkzeug.routing import BuildError, Map, RequestRedirect, \
RoutingException, Rule

from . import cli, json
from ._compat import integer_types, reraise, string_types, text_type
Expand Down Expand Up @@ -1631,13 +1632,29 @@ def handle_http_exception(self, e):
registered error handlers and fall back to returning the
exception as response.
.. versionchanged:: 1.0.3
``RoutingException``, used internally for actions such as
slash redirects during routing, is not passed to error
handlers.
.. versionchanged:: 1.0
Exceptions are looked up by code *and* by MRO, so
``HTTPExcpetion`` subclasses can be handled with a catch-all
handler for the base ``HTTPException``.
.. versionadded:: 0.3
"""
# Proxy exceptions don't have error codes. We want to always return
# those unchanged as errors
if e.code is None:
return e

# RoutingExceptions are used internally to trigger routing
# actions, such as slash redirects raising RequestRedirect. They
# are not raised or handled in user code.
if isinstance(e, RoutingException):
return e

handler = self._find_error_handler(e)
if handler is None:
return e
Expand Down
8 changes: 6 additions & 2 deletions tests/test_user_error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,16 @@ def catchall_errorhandler(e):
def forbidden():
raise Forbidden()

@app.route("/slash/")
def slash():
return "slash"

app.register_blueprint(bp, url_prefix='/bp')

c = app.test_client()
assert c.get('/bp/undefined').data == b'bp-default'
assert c.get('/bp/forbidden').data == b'bp-forbidden'
assert c.get('/undefined').data == b'default'
assert c.get('/forbidden').data == b'forbidden'


# Don't handle RequestRedirect raised when adding slash.
assert c.get("/slash", follow_redirects=True).data == b"slash"

0 comments on commit ded7cbe

Please sign in to comment.