diff --git a/flask/wrappers.py b/flask/wrappers.py index abcf90f8..de5b21a1 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -15,7 +15,6 @@ from werkzeug.exceptions import BadRequest from . import json from .globals import _request_ctx_stack - _missing = object() @@ -175,6 +174,9 @@ class Request(RequestBase): .. versionadded:: 0.8 """ + ctx = _request_ctx_stack.top + 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): diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 7b5b32f2..8d09327f 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_response_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 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__) + 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 b'Failed to decode JSON object' not in rv.data + def test_json_bad_requests(self): app = flask.Flask(__name__) @app.route('/json', methods=['POST'])