Skip to content

Handle non-JSON responses #105

@blacklight

Description

@blacklight

The upstream API may sometimes return non-JSON responses (usually HTML or text) upon non-2xx HTTP response codes.

This can be the case for errors such as rate limit exceeded or temporary 5xx errors. When this happens, tidalapi breaks like this:

...
  File "~/venv/venv/lib/python3.10/site-packages/tidalapi/session.py", line 504, in album
    return tidalapi.Album(session=self, album_id=album_id)
  File "~/venv/venv/lib/python3.10/site-packages/tidalapi/album.py", line 59, in __init__
    self.requests.map_request('albums/%s' % album_id, parse=self.parse)
  File "~/venv/venv/lib/python3.10/site-packages/tidalapi/request.py", line 104, in map_request
    json_obj = self.request('GET', url, params).json()
  File "~/venv/venv/lib/python3.10/site-packages/tidalapi/request.py", line 85, in request
    request = self.basic_request(method, path, params, data, headers)
  File "~/venv/venv/lib/python3.10/site-packages/tidalapi/request.py", line 63, in basic_request
    if not request.ok and request.json()['userMessage'].startswith("The token has expired.") and refresh_token:
  File "~/venv/venv/lib/python3.10/site-packages/requests/models.py", line 917, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: [Errno Expecting value] : 0

The code basically expects the response to be always valid JSON, hence request.json() fails as soon as it checks for a specific JSON field.

We should wrap the JSON decode logic in a try/except block, and let the consumer handle the error appropriately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions