From b573a86977161b93152dddb5bfc43195335b3c59 Mon Sep 17 00:00:00 2001 From: David Lord Date: Sat, 28 Apr 2018 06:51:08 -0700 Subject: [PATCH] trap key errors in debug, not all 400 errors --- flask/app.py | 10 ++++++++-- tests/test_basic.py | 25 +++++++++++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/flask/app.py b/flask/app.py index 2de526e2..3c106ee8 100644 --- a/flask/app.py +++ b/flask/app.py @@ -1663,8 +1663,14 @@ class Flask(_PackageBoundObject): trap_bad_request = self.config['TRAP_BAD_REQUEST_ERRORS'] - # if unset, trap based on debug mode - if (trap_bad_request is None and self.debug) or trap_bad_request: + # if unset, trap key errors in debug mode + if ( + trap_bad_request is None and self.debug + and isinstance(e, BadRequestKeyError) + ): + return True + + if trap_bad_request: return isinstance(e, BadRequest) return False diff --git a/tests/test_basic.py b/tests/test_basic.py index 9d6c58c1..c0168ae3 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1027,21 +1027,34 @@ def test_errorhandler_precedence(app, client): def test_trapping_of_bad_request_key_errors(app, client): - @app.route('/fail') + @app.route('/key') def fail(): flask.request.form['missing_key'] - rv = client.get('/fail') + @app.route('/abort') + def allow_abort(): + flask.abort(400) + + rv = client.get('/key') assert rv.status_code == 400 assert b'missing_key' not in rv.data + rv = client.get('/abort') + assert rv.status_code == 400 - app.config['TRAP_BAD_REQUEST_ERRORS'] = True - + app.debug = True with pytest.raises(KeyError) as e: - client.get("/fail") - + client.get("/key") assert e.errisinstance(BadRequest) assert 'missing_key' in e.value.description + rv = client.get('/abort') + assert rv.status_code == 400 + + app.debug = False + app.config['TRAP_BAD_REQUEST_ERRORS'] = True + with pytest.raises(KeyError): + client.get('/key') + with pytest.raises(BadRequest): + client.get('/abort') def test_trapping_of_all_http_exceptions(app, client):