mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-22 09:37:07 +03:00
Merge pull request #145 from dotsbb/fix-too-broad-exception
Wrong error message "invalid JSON" when "RequestDataTooBig" raised
This commit is contained in:
commit
3667157e4a
|
@ -457,6 +457,21 @@ def test_handles_invalid_json_bodies(client):
|
|||
}
|
||||
|
||||
|
||||
def test_handles_django_request_error(client, monkeypatch):
|
||||
def mocked_read(*args):
|
||||
raise IOError("foo-bar")
|
||||
|
||||
monkeypatch.setattr("django.http.request.HttpRequest.read", mocked_read)
|
||||
|
||||
valid_json = json.dumps(dict(foo='bar'))
|
||||
response = client.post(url_string(), valid_json, 'application/json')
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response_json(response) == {
|
||||
'errors': [{'message': 'foo-bar'}]
|
||||
}
|
||||
|
||||
|
||||
def test_handles_incomplete_json_bodies(client):
|
||||
response = client.post(url_string(), '{"query":', 'application/json')
|
||||
|
||||
|
|
|
@ -179,7 +179,6 @@ class GraphQLView(View):
|
|||
return json.dumps(d, sort_keys=True,
|
||||
indent=2, separators=(',', ': '))
|
||||
|
||||
# noinspection PyBroadException
|
||||
def parse_body(self, request):
|
||||
content_type = self.get_content_type(request)
|
||||
|
||||
|
@ -187,8 +186,14 @@ class GraphQLView(View):
|
|||
return {'query': request.body.decode()}
|
||||
|
||||
elif content_type == 'application/json':
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
request_json = json.loads(request.body.decode('utf-8'))
|
||||
body = request.body.decode('utf-8')
|
||||
except Exception as e:
|
||||
raise HttpError(HttpResponseBadRequest(str(e)))
|
||||
|
||||
try:
|
||||
request_json = json.loads(body)
|
||||
if self.batch:
|
||||
assert isinstance(request_json, list), (
|
||||
'Batch requests should receive a list, but received {}.'
|
||||
|
@ -203,7 +208,7 @@ class GraphQLView(View):
|
|||
return request_json
|
||||
except AssertionError as e:
|
||||
raise HttpError(HttpResponseBadRequest(str(e)))
|
||||
except:
|
||||
except (TypeError, ValueError):
|
||||
raise HttpError(HttpResponseBadRequest('POST body sent invalid JSON.'))
|
||||
|
||||
elif content_type in ['application/x-www-form-urlencoded', 'multipart/form-data']:
|
||||
|
|
Loading…
Reference in New Issue
Block a user