Skip to content

Commit 35b00c7

Browse files
committed
Add catching ValueError: No JSON object could be decoded
1 parent 3666e22 commit 35b00c7

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

intercom/intercom.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def wrapper(*args, **kwargs):
4545
response = func_to_decorate(*args, **kwargs)
4646
if response.status_code == 401:
4747
raise AuthenticationError("Invalid API key/username provided.")
48-
result = json.loads(response.content)
48+
try:
49+
result = json.loads(response.content)
50+
except ValueError as err:
51+
raise ServerError(err.message)
4952
if response.status_code in (200, 201):
5053
pass
5154
else:

tests/unit/fixtures/invalid.json

Whitespace-only changes.

tests/unit/test_intercom.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,8 @@ def test_not_found(self):
7777
@patch('requests.request', create_response(500, '500.json'))
7878
def test_api_error(self):
7979
resp = Intercom.get_users()
80+
81+
@raises(ServerError)
82+
@patch('requests.request', create_response(500, 'invalid.json'))
83+
def test_api_error_when_json_is_invalid(self):
84+
Intercom.get_users()

0 commit comments

Comments
 (0)