Skip to content

Wrong error message "invalid JSON" when "RequestDataTooBig" raised #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions graphene_django/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
11 changes: 8 additions & 3 deletions graphene_django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,21 @@ def json_encode(self, request, d, pretty=False):
return json.dumps(d, sort_keys=True,
indent=2, separators=(',', ': '))

# noinspection PyBroadException
def parse_body(self, request):
content_type = self.get_content_type(request)

if content_type == 'application/graphql':
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 {}.'
Expand All @@ -203,7 +208,7 @@ def parse_body(self, request):
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']:
Expand Down