Browse Source

Don't only catch BadRequest key errors but all bad request errors.

pull/302/head
Armin Ronacher 14 years ago
parent
commit
acac64e36a
  1. 14
      docs/config.rst
  2. 8
      flask/app.py
  3. 2
      tests/flask_tests.py

14
docs/config.rst

@ -89,13 +89,15 @@ The following configuration values are used internally by Flask:
helpful for hairy debugging situations
where you have to find out where an HTTP
exception is coming from.
``TRAP_BAD_REQUEST_KEY_ERRORS`` Werkzeug's internal data structures that
``TRAP_BAD_REQUEST_ERRORS`` Werkzeug's internal data structures that
deal with request specific data will
raise special key errors that are also
bad request exceptions. By default
these will be converted into 400
responses which however can make
debugging some issues harder. If this
bad request exceptions. Likewise many
operations can implicitly fail with a
BadRequest exception for consistency.
Since it's nice for debugging to know
why exactly it failed this flag can be
used to debug those situations. If this
config is set to ``True`` you will get
a regular traceback instead.
================================= =========================================
@ -132,7 +134,7 @@ The following configuration values are used internally by Flask:
``PROPAGATE_EXCEPTIONS``, ``PRESERVE_CONTEXT_ON_EXCEPTION``
.. versionadded:: 0.8
``TRAP_BAD_REQUEST_KEY_ERRORS``, ``TRAP_HTTP_EXCEPTIONS``
``TRAP_BAD_REQUEST_ERRORS``, ``TRAP_HTTP_EXCEPTIONS``
Configuring from Files
----------------------

8
flask/app.py

@ -216,7 +216,7 @@ class Flask(_PackageBoundObject):
'LOGGER_NAME': None,
'SERVER_NAME': None,
'MAX_CONTENT_LENGTH': None,
'TRAP_BAD_REQUEST_KEY_ERRORS': False,
'TRAP_BAD_REQUEST_ERRORS': False,
'TRAP_HTTP_EXCEPTIONS': False
})
@ -1054,7 +1054,7 @@ class Flask(_PackageBoundObject):
def trap_http_exception(self, e):
"""Checks if an HTTP exception should be trapped or not. By default
this will return `False` for all exceptions except for a bad request
key error if ``TRAP_BAD_REQUEST_KEY_ERRORS`` is set to `True`. It
key error if ``TRAP_BAD_REQUEST_ERRORS`` is set to `True`. It
also returns `True` if ``TRAP_HTTP_EXCEPTIONS`` is set to `True`.
This is called for all HTTP exceptions raised by a view function.
@ -1067,8 +1067,8 @@ class Flask(_PackageBoundObject):
"""
if self.config['TRAP_HTTP_EXCEPTIONS']:
return True
if self.config['TRAP_BAD_REQUEST_KEY_ERRORS']:
return isinstance(e, BadRequest) and isinstance(e, LookupError)
if self.config['TRAP_BAD_REQUEST_ERRORS']:
return isinstance(e, BadRequest)
return False
def handle_user_exception(self, e):

2
tests/flask_tests.py

@ -601,7 +601,7 @@ class BasicFunctionalityTestCase(unittest.TestCase):
c = app.test_client()
assert c.get('/fail').status_code == 400
app.config['TRAP_BAD_REQUEST_KEY_ERRORS'] = True
app.config['TRAP_BAD_REQUEST_ERRORS'] = True
c = app.test_client()
try:
c.get('/fail')

Loading…
Cancel
Save