Skip to content

Commit b072909

Browse files
authored
Merge pull request #571 from ably/fix-error-decoding
util: Fix decoding msgpack encoded error responses
2 parents 4066ce3 + de91d21 commit b072909

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

ably/util/exceptions.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import logging
3+
import msgpack
34

45

56
log = logging.getLogger(__name__)
@@ -35,17 +36,17 @@ def raise_for_response(response):
3536
return
3637

3738
try:
38-
json_response = response.json()
39+
decoded_response = AblyException.decode_error_response(response)
3940
except Exception:
40-
log.debug("Response not json: %d %s",
41+
log.debug("Response not json or msgpack: %d %s",
4142
response.status_code,
4243
response.text)
4344
raise AblyException(message=response.text,
4445
status_code=response.status_code,
4546
code=response.status_code * 100)
4647

47-
if json_response and 'error' in json_response:
48-
error = json_response['error']
48+
if decoded_response and 'error' in decoded_response:
49+
error = decoded_response['error']
4950
try:
5051
raise AblyException(
5152
message=error['message'],
@@ -61,6 +62,17 @@ def raise_for_response(response):
6162
status_code=response.status_code,
6263
code=response.status_code * 100)
6364

65+
@staticmethod
66+
def decode_error_response(response):
67+
content_type = response.headers.get('content-type')
68+
if isinstance(content_type, str):
69+
if content_type.startswith('application/x-msgpack'):
70+
return msgpack.unpackb(response.content)
71+
elif content_type.startswith('application/json'):
72+
return response.json()
73+
74+
raise ValueError("Unsupported content type")
75+
6476
@staticmethod
6577
def from_exception(e):
6678
if isinstance(e, AblyException):

0 commit comments

Comments
 (0)