Complain if API response is not JSON (#40966)

This commit is contained in:
Nathaniel Case
2018-05-31 13:20:17 -04:00
committed by GitHub
parent da4ff18406
commit 91fd98a2bd
2 changed files with 11 additions and 2 deletions

View File

@@ -34,7 +34,11 @@ class HttpApi:
headers = {'Content-Type': 'application/json-rpc'}
response = self.connection.send('/command-api', request, headers=headers, method='POST')
response = json.loads(to_text(response.read()))
response_text = to_text(response.read())
try:
response = json.loads(response_text)
except ValueError:
raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text))
results = handle_response(response)
if self._become:

View File

@@ -29,7 +29,12 @@ class HttpApi:
headers = {'Content-Type': 'application/json'}
response = self.connection.send('/ins', request, headers=headers, method='POST')
response = json.loads(to_text(response.read()))
response_text = to_text(response.read())
try:
response = json.loads(response_text)
except ValueError:
raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text))
results = handle_response(response)
if self._become: