From 5d3e7b737c834595735cbeb81077739dd950f396 Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Sat, 28 Feb 2015 01:22:15 -0500 Subject: [PATCH 01/10] Add exception details to response --- flask/wrappers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask/wrappers.py b/flask/wrappers.py index abcf90f8..759e9905 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -175,7 +175,7 @@ class Request(RequestBase): .. versionadded:: 0.8 """ - raise BadRequest() + raise BadRequest(e) def _load_form_data(self): RequestBase._load_form_data(self) From 82b7983b2a0262645dd4dcb72c441b7affa816be Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Mon, 2 Mar 2015 12:12:37 -0500 Subject: [PATCH 02/10] Make exception contents conditional on DEBUG --- flask/wrappers.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/flask/wrappers.py b/flask/wrappers.py index 759e9905..9f7a6420 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -13,8 +13,7 @@ from werkzeug.wrappers import Request as RequestBase, Response as ResponseBase from werkzeug.exceptions import BadRequest from . import json -from .globals import _request_ctx_stack - +from .globals import _request_ctx_stack, current_app _missing = object() @@ -175,7 +174,10 @@ class Request(RequestBase): .. versionadded:: 0.8 """ - raise BadRequest(e) + if current_app.config['DEBUG']: + raise BadRequest(e) + else: + raise BadRequest() def _load_form_data(self): RequestBase._load_form_data(self) From 19df249c8907d79d58b8c9dc5fa5cea6e40ad2e7 Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Wed, 4 Mar 2015 13:21:39 -0500 Subject: [PATCH 03/10] Change logic to support when current_app is not available --- flask/wrappers.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/flask/wrappers.py b/flask/wrappers.py index 9f7a6420..57d5a029 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -174,10 +174,11 @@ class Request(RequestBase): .. versionadded:: 0.8 """ - if current_app.config['DEBUG']: - raise BadRequest(e) - else: - raise BadRequest() + ctx = _request_ctx_stack.top + if ctx is not None: + if ctx.app.config.get('DEBUG', False): + raise BadRequest(e) + raise BadRequest() def _load_form_data(self): RequestBase._load_form_data(self) From 39e66ca6d782630c67eae4e7e70e60799386ec8b Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Wed, 4 Mar 2015 14:40:16 -0500 Subject: [PATCH 04/10] Add tests for adding exception to response contents only when DEBUG is True --- tests/test_helpers.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 7b5b32f2..015bb08d 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -49,6 +49,30 @@ class TestJSON(object): assert rv.mimetype == 'application/json' assert flask.json.loads(rv.data)['x'] == http_date(d.timetuple()) + def test_post_empty_json_adds_exception_to_reponse_content_in_debug(self): + app = flask.Flask(__name__) + app.config['DEBUG'] = True + @app.route('/json', methods=['POST']) + def post_json(): + flask.request.get_json() + return None + c = app.test_client() + rv = c.post('/json', data=None, content_type='application/json') + assert rv.status_code == 400 + assert '

No JSON object could be decoded

' in rv.data + + def test_post_empty_json_doesnt_add_exception_to_reponse_if_no_debug(self): + app = flask.Flask(__name__) + app.config['DEBUG'] = False + @app.route('/json', methods=['POST']) + def post_json(): + flask.request.get_json() + return None + c = app.test_client() + rv = c.post('/json', data=None, content_type='application/json') + assert rv.status_code == 400 + assert '

No JSON object could be decoded

' not in rv.data + def test_json_bad_requests(self): app = flask.Flask(__name__) @app.route('/json', methods=['POST']) From 294961e6fce2c34784f304ca587850d62569b64e Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Wed, 4 Mar 2015 14:48:08 -0500 Subject: [PATCH 05/10] Change tests to support older response format --- tests/test_helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 015bb08d..c9a02362 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -59,7 +59,7 @@ class TestJSON(object): c = app.test_client() rv = c.post('/json', data=None, content_type='application/json') assert rv.status_code == 400 - assert '

No JSON object could be decoded

' in rv.data + assert 'No JSON object could be decoded' in rv.data def test_post_empty_json_doesnt_add_exception_to_reponse_if_no_debug(self): app = flask.Flask(__name__) @@ -71,7 +71,7 @@ class TestJSON(object): c = app.test_client() rv = c.post('/json', data=None, content_type='application/json') assert rv.status_code == 400 - assert '

No JSON object could be decoded

' not in rv.data + assert 'No JSON object could be decoded' not in rv.data def test_json_bad_requests(self): app = flask.Flask(__name__) From d6a1307f9debe7f878961866d42c380ade6b04da Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Wed, 4 Mar 2015 15:14:43 -0500 Subject: [PATCH 06/10] Change strings to bytes to support Python3, typo in function name --- tests/test_helpers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index c9a02362..5867ab65 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -49,7 +49,7 @@ class TestJSON(object): assert rv.mimetype == 'application/json' assert flask.json.loads(rv.data)['x'] == http_date(d.timetuple()) - def test_post_empty_json_adds_exception_to_reponse_content_in_debug(self): + def test_post_empty_json_adds_exception_to_response_content_in_debug(self): app = flask.Flask(__name__) app.config['DEBUG'] = True @app.route('/json', methods=['POST']) @@ -59,9 +59,9 @@ class TestJSON(object): c = app.test_client() rv = c.post('/json', data=None, content_type='application/json') assert rv.status_code == 400 - assert 'No JSON object could be decoded' in rv.data + assert b'No JSON object could be decoded' in rv.data - def test_post_empty_json_doesnt_add_exception_to_reponse_if_no_debug(self): + def test_post_empty_json_wont_add_exception_to_response_if_no_debug(self): app = flask.Flask(__name__) app.config['DEBUG'] = False @app.route('/json', methods=['POST']) @@ -71,7 +71,7 @@ class TestJSON(object): c = app.test_client() rv = c.post('/json', data=None, content_type='application/json') assert rv.status_code == 400 - assert 'No JSON object could be decoded' not in rv.data + assert b'No JSON object could be decoded' not in rv.data def test_json_bad_requests(self): app = flask.Flask(__name__) From 290c371eb1d0a140203cffce6f270b3cca6783dc Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Thu, 5 Mar 2015 12:48:38 -0500 Subject: [PATCH 07/10] Change to hardcoding exception contents to support Python3 json loading exception format --- flask/wrappers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask/wrappers.py b/flask/wrappers.py index 57d5a029..df9d205c 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -177,7 +177,7 @@ class Request(RequestBase): ctx = _request_ctx_stack.top if ctx is not None: if ctx.app.config.get('DEBUG', False): - raise BadRequest(e) + raise BadRequest('No JSON object could be decoded') raise BadRequest() def _load_form_data(self): From 1cd9e91810fcfc3b3ca4e8c157593af28de427d6 Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Thu, 5 Mar 2015 13:04:38 -0500 Subject: [PATCH 08/10] Changed error message to include actual exception contents --- flask/wrappers.py | 2 +- tests/test_helpers.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/flask/wrappers.py b/flask/wrappers.py index df9d205c..fe0c7bcb 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -177,7 +177,7 @@ class Request(RequestBase): ctx = _request_ctx_stack.top if ctx is not None: if ctx.app.config.get('DEBUG', False): - raise BadRequest('No JSON object could be decoded') + raise BadRequest('Failed to decode JSON object: {}'.format(e)) raise BadRequest() def _load_form_data(self): diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 5867ab65..8d09327f 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -59,7 +59,7 @@ class TestJSON(object): c = app.test_client() rv = c.post('/json', data=None, content_type='application/json') assert rv.status_code == 400 - assert b'No JSON object could be decoded' in rv.data + assert b'Failed to decode JSON object' in rv.data def test_post_empty_json_wont_add_exception_to_response_if_no_debug(self): app = flask.Flask(__name__) @@ -71,7 +71,7 @@ class TestJSON(object): c = app.test_client() rv = c.post('/json', data=None, content_type='application/json') assert rv.status_code == 400 - assert b'No JSON object could be decoded' not in rv.data + assert b'Failed to decode JSON object' not in rv.data def test_json_bad_requests(self): app = flask.Flask(__name__) From 44da905cb7e0d81267d75ccfcb7ffa2669f02aeb Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Thu, 5 Mar 2015 13:19:00 -0500 Subject: [PATCH 09/10] Change format index to support Python 2.6 --- flask/wrappers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask/wrappers.py b/flask/wrappers.py index fe0c7bcb..c3b16bc6 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -177,7 +177,7 @@ class Request(RequestBase): ctx = _request_ctx_stack.top if ctx is not None: if ctx.app.config.get('DEBUG', False): - raise BadRequest('Failed to decode JSON object: {}'.format(e)) + raise BadRequest('Failed to decode JSON object: {0}'.format(e)) raise BadRequest() def _load_form_data(self): From 7b080355f561333e4eea01f656f0e4b32dcc688c Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Thu, 5 Mar 2015 13:26:59 -0500 Subject: [PATCH 10/10] Remove unused import and clean control flow --- flask/wrappers.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/flask/wrappers.py b/flask/wrappers.py index c3b16bc6..de5b21a1 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -13,7 +13,7 @@ from werkzeug.wrappers import Request as RequestBase, Response as ResponseBase from werkzeug.exceptions import BadRequest from . import json -from .globals import _request_ctx_stack, current_app +from .globals import _request_ctx_stack _missing = object() @@ -175,9 +175,8 @@ class Request(RequestBase): .. versionadded:: 0.8 """ ctx = _request_ctx_stack.top - if ctx is not None: - if ctx.app.config.get('DEBUG', False): - raise BadRequest('Failed to decode JSON object: {0}'.format(e)) + if ctx is not None and ctx.app.config.get('DEBUG', False): + raise BadRequest('Failed to decode JSON object: {0}'.format(e)) raise BadRequest() def _load_form_data(self):