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
Provenance
- Commit
ce70131- Tree
6611daa- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
acac64e
4 files changed+36 added−4 removed
modified:docs/upgrading.rst+7 added
27 unmodified lines28 object. With that introduction we moved the implementation details for29 the session system into a new module called :mod:`flask.sessions`. If you30 used the previously undocumented session support we urge you to upgrade.31 32+If invalid JSON data was submitted Flask will now raise a33+:exc:`~werkzeug.exceptions.BadRequest` exception instead of letting the34+default :exc:`ValueError` bubble up. This has the advantage that you no35+longer have to handle that error to avoid an internal server error showing36+up for the user. If you were catching this down explicitly in the past37+as `ValueError` you will need to change this.38+39 Version 0.740 -----------41 42 In Flask 0.7 we cleaned up the code base internally a lot and did some276 unmodified linesmodified:flask/wrappers.py+16 added-3 removed
8 unmodified lines9 :license: BSD, see LICENSE for more details.10 """11 12 from werkzeug.wrappers import Request as RequestBase, Response as ResponseBase13+from werkzeug.exceptions import BadRequest14 from werkzeug.utils import cached_property15 16 from .debughelpers import attach_enctype_error_multidict17 from .helpers import json, _assert_have_json78 unmodified lines96 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)106 107+ 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`.111+112+ .. versionadded:: 0.8113+ """114+ raise BadRequest()115+116 def _load_form_data(self):117 RequestBase._load_form_data(self)118 119 # in debug mode we're replacing the files multidict with an ad-hoc17 unmodified linesmodified:tests/flask_tests.py+9 added
982 unmodified lines983 984 985 class JSONTestCase(unittest.TestCase):986 987+ 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)995+996 def test_json_body_encoding(self):997 app = flask.Flask(__name__)998 app.testing = True999 @app.route('/')1086 unmodified linesmodified:CHANGES+4 added-1 removed
12 unmodified lines13 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.29 30 Version 0.7.331 -------------32 238 unmodified lines