Skip to content
Myr

Merge pull request #3063 from pallets/key-error

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

clear KeyError in production for Werkzeug 0.15

authored by David Lord · committed by GitHub · parents 1096e22, 49efc44 · Browse files · 2 files+12 added−11 removed
This commit

State

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

Diff stats

2 files+12 added−11 removed

  • tests:+1 added−1 removed
  • other:+11 added−10 removed

2 files+12 added−11 removed

modified:flask/app.py+11 added-10 removed
1696 unmodified lines
1697 # wants the traceback preserved in handle_http_exception. Of course1698 # we cannot prevent users from trashing it themselves in a custom1699 # trap_http_exception method so that's their fault then.17001701 # MultiDict passes the key to the exception, but that's ignored1702 # when generating the response message. Set an informative1703 # description for key errors in debug mode or when trapping errors.1704 if (1705 (self.debug or self.config['TRAP_BAD_REQUEST_ERRORS'])1706 and isinstance(e, BadRequestKeyError)1707 # only set it if it's still the default description1708 and e.description is BadRequestKeyError.description1709 ):1710 e.description = "KeyError: '{0}'".format(*e.args)1701 if isinstance(e, BadRequestKeyError):1702 if self.debug or self.config["TRAP_BAD_REQUEST_ERRORS"]:1703 # Werkzeug < 0.15 doesn't add the KeyError to the 4001704 # message, add it in manually.1705 description = e.get_description()17061707 if e.args[0] not in description:1708 e.description = "KeyError: '{}'".format(*e.args)1709 else:1710 # Werkzeug >= 0.15 does add it, remove it in production1711 e.args = ()17121713 if isinstance(e, HTTPException) and not self.trap_http_exception(e):1714 return self.handle_http_exception(e)17151716 handler = self._find_error_handler(e)
600 unmodified lines
modified:tests/test_basic.py+1 added-1 removed
1043 unmodified lines
1044 app.debug = True1045 with pytest.raises(KeyError) as e:1046 client.get("/key")1047 assert e.errisinstance(BadRequest)1048 assert 'missing_key' in e.value.description1048 assert 'missing_key' in e.value.get_description()1049 rv = client.get('/abort')1050 assert rv.status_code == 40010511052 app.debug = False
910 unmodified lines