Skip to content
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

No JSON could be decoded error. #137

Merged
merged 1 commit into from
May 10, 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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This document describes changes between each past release.

- Fix retry behaviour when responses are successful (fixes #129)
- Fix Retry-After value to be read as integer rather than string. (#131)
- Fix No JSON could be decoded ValueError (fixes #116)

**Internal changes**

Expand Down
6 changes: 5 additions & 1 deletion kinto_http/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ def request(self, method, endpoint, data=None, permissions=None,
continue

# Retries exhausted, raise expection.
message = '{0} - {1}'.format(resp.status_code, resp.json())
try:
message = '{0} - {1}'.format(resp.status_code, resp.json())
except ValueError:
# In case the response is not JSON, fallback to text.
message = '{0} - {1}'.format(resp.status_code, resp.text)
exception = KintoException(message)
exception.request = resp.request
exception.response = resp
Expand Down
11 changes: 11 additions & 0 deletions kinto_http/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ def test_bad_http_status_raises_exception(self):

self.assertRaises(KintoException, session.request, 'get', '/test')

def test_bad_http_status_raises_exception_even_in_case_of_invalid_json_response(self):
response = fake_response(502)
response.json.side_effect = ValueError
response.text = "Foobar"
self.requests_mock.request.return_value = response
session = Session('https://example.org')

with pytest.raises(KintoException) as e:
session.request('get', '/test')
self.assertEqual(e.value.message, "502 - Foobar")

def test_session_injects_auth_on_requests(self):
response = fake_response(200)
self.requests_mock.request.return_value = response
Expand Down