Skip to content
Myr

If JSON parsing fails it now issues a BadRequest exception.

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

authored by Armin Ronacher · committed by Armin Ronacher · parent acac64e · Browse files · 4 files+36 added−4 removed
This commit

State

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

Diff stats

4 files+36 added−4 removed

  • tests:+9 added−0 removed
  • other:+27 added−4 removed

4 files+36 added−4 removed

modified:docs/upgrading.rst+7 added
27 unmodified lines
28object. With that introduction we moved the implementation details for29the session system into a new module called :mod:`flask.sessions`. If you30used the previously undocumented session support we urge you to upgrade.3132If invalid JSON data was submitted Flask will now raise a33:exc:`~werkzeug.exceptions.BadRequest` exception instead of letting the34default :exc:`ValueError` bubble up. This has the advantage that you no35longer have to handle that error to avoid an internal server error showing36up for the user. If you were catching this down explicitly in the past37as `ValueError` you will need to change this.3839Version 0.740-----------4142In Flask 0.7 we cleaned up the code base internally a lot and did some
276 unmodified lines
modified:flask/wrappers.py+16 added-3 removed
8 unmodified lines
9 :license: BSD, see LICENSE for more details.10"""1112from werkzeug.wrappers import Request as RequestBase, Response as ResponseBase13from werkzeug.exceptions import BadRequest14from werkzeug.utils import cached_property1516from .debughelpers import attach_enctype_error_multidict17from .helpers import json, _assert_have_json
78 unmodified lines
96 if __debug__:97 _assert_have_json()98 if self.mimetype == 'application/json':99 request_charset = self.mimetype_params.get('charset')100 try:99 if request_charset is not None:100 return json.loads(self.data, encoding=request_charset)101 return json.loads(self.data)101 if request_charset is not None:102 return json.loads(self.data, encoding=request_charset)103 return json.loads(self.data)104 except ValueError, e:105 return self.on_json_loading_failed(e)106107 def on_json_loading_failed(self, e):108 """Called if decoding of the JSON data failed. The return value of109 this method is used by :attr:`json` when an error ocurred. The110 default implementation raises a :class:`~werkzeug.exceptions.BadRequest`.111112 .. versionadded:: 0.8113 """114 raise BadRequest()115116 def _load_form_data(self):117 RequestBase._load_form_data(self)118119 # in debug mode we're replacing the files multidict with an ad-hoc
17 unmodified lines
modified:tests/flask_tests.py+9 added
982 unmodified lines
983984985class JSONTestCase(unittest.TestCase):986987 def test_json_bad_requests(self):988 app = flask.Flask(__name__)989 @app.route('/json', methods=['POST'])990 def return_json():991 return unicode(flask.request.json)992 c = app.test_client()993 rv = c.post('/json', data='malformed', content_type='application/json')994 self.assertEqual(rv.status_code, 400)995996 def test_json_body_encoding(self):997 app = flask.Flask(__name__)998 app.testing = True999 @app.route('/')
1086 unmodified lines
modified:CHANGES+4 added-1 removed
12 unmodified lines
13 having to override the Flask class.14- Empty session cookies are now deleted properly automatically.15- View functions can now opt out of getting the automatic16 OPTIONS implementation.17- HTTP exceptions and Bad Request Key Errors can now be trapped so that they17- HTTP exceptions and Bad Request errors can now be trapped so that they18 show up normally in the traceback.19- Flask in debug mode is now detecting some common problems and tries to20 warn you about them.21- Flask in debug mode will now complain with an assertion error if a view22 was attached after the first request was handled. This gives earlier23 feedback when users forget to import view code ahead of time.24- Added the ability to register callbacks that are only triggered once at25 the beginning of the first request. (:meth:`Flask.before_first_request`)26- Malformed JSON data will now trigger a bad request HTTP exception instead27 of a value error which usually would result in a 500 internal server28 error if not handled. This is a backwards incompatible change.2930Version 0.7.331-------------32
238 unmodified lines