Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty deque on errorhandler lookup. #1480

Merged
merged 1 commit into from Jun 3, 2015

Conversation

jmcarp
Copy link
Contributor

@jmcarp jmcarp commented Jun 2, 2015

After registering a custom errorhandler by exception class, raising any
unhandled exception in a view function swallows the error and instead
throws an IndexError on trying to look up the appropriate handler.
This patch avoids the uninformative IndexError and preserves the
original exception by looping until the deque of classes is empty, not
forever.

After registering a custom errorhandler by exception class, raising any
unhandled exception in a view function swallows the error and instead
throws an `IndexError` on trying to look up the appropriate handler.
This patch avoids the uninformative `IndexError` and preserves the
original exception by looping until the deque of classes is empty, not
forever.
@jmcarp
Copy link
Contributor Author

jmcarp commented Jun 2, 2015

Code example:

from flask import Flask

app = Flask(__name__)

@app.errorhandler(TypeError)
def handle_typeerror(e):
    return 'got a typeerror!'

@app.route('/keyerror')
def keyerror():
    raise KeyError()

c = app.test_client()
c.get('/keyerror')

Since we've registered a handler for TypeError and not for KeyError, I would expect the KeyError to be thrown here. But instead we get an IndexError with no hint of the original error:

  File "/Users/jmcarp/code/flask/flask/app.py", line 1951, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/jmcarp/code/flask/flask/app.py", line 1605, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/jmcarp/code/flask/flask/app.py", line 1505, in handle_user_exception
    handler = self._find_error_handler(e)
  File "/Users/jmcarp/code/flask/flask/app.py", line 1440, in _find_error_handler
    .get(code))
  File "/Users/jmcarp/code/flask/flask/app.py", line 1425, in find_handler
    cls = queue.popleft()
IndexError: pop from an empty deque

With the proposed patch, the expected KeyError is thrown instead.

untitaker added a commit that referenced this pull request Jun 3, 2015
Handle empty deque on errorhandler lookup.
@untitaker untitaker merged commit 5ebf1e8 into pallets:master Jun 3, 2015
@untitaker
Copy link
Contributor

Excellent, thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants