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

util: Fix decoding msgpack encoded error responses #571

Merged
merged 1 commit into from
Oct 1, 2024
Merged
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
20 changes: 16 additions & 4 deletions ably/util/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import logging
import msgpack


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -35,17 +36,17 @@ def raise_for_response(response):
return

try:
json_response = response.json()
decoded_response = AblyException.decode_error_response(response)
except Exception:
log.debug("Response not json: %d %s",
log.debug("Response not json or msgpack: %d %s",
response.status_code,
response.text)
raise AblyException(message=response.text,
status_code=response.status_code,
code=response.status_code * 100)

if json_response and 'error' in json_response:
error = json_response['error']
if decoded_response and 'error' in decoded_response:
error = decoded_response['error']
try:
raise AblyException(
message=error['message'],
Expand All @@ -61,6 +62,17 @@ def raise_for_response(response):
status_code=response.status_code,
code=response.status_code * 100)

@staticmethod
def decode_error_response(response):
content_type = response.headers.get('content-type')
if isinstance(content_type, str):
if content_type.startswith('application/x-msgpack'):
return msgpack.unpackb(response.content)
lmars marked this conversation as resolved.
Show resolved Hide resolved
elif content_type.startswith('application/json'):
return response.json()

raise ValueError("Unsupported content type")
lmars marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def from_exception(e):
if isinstance(e, AblyException):
Expand Down
Loading