Skip to content

Errors are not handled properly in async mode #24

@ngseer

Description

@ngseer

In async mode session.get() raises an AttributeError during error handling.

Here's an example of code reproducing the bug:

>>> import asyncio
>>> loop = asyncio.get_event_loop()
>>> from jsonapi_client import Session
>>> session = Session('http://localhost:8080', enable_async=True)
>>> loop.run_until_complete(session.get('invalid-resource'))  # Server will generate a 404 error on that request
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib64/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "/home/seer/.local/share/virtualenvs/pbJ32E1R/lib/python3.7/site-packages/jsonapi_client/session.py", line 345, in _get_async
    return await self.fetch_document_by_url_async(url)
  File "/home/seer/.local/share/virtualenvs/pbJ32E1R/lib/python3.7/site-packages/jsonapi_client/session.py", line 466, in fetch_document_by_url_async
    await self._ext_fetch_by_url_async(url))
  File "/home/seer/.local/share/virtualenvs/pbJ32E1R/lib/python3.7/site-packages/jsonapi_client/session.py", line 473, in _ext_fetch_by_url_async
    json_data = await self._fetch_json_async(url)
  File "/home/seer/.local/share/virtualenvs/pbJ32E1R/lib/python3.7/site-packages/jsonapi_client/session.py", line 510, in _fetch_json_async
    raise DocumentError(f'Error {response.status_code}: '
AttributeError: 'ClientResponse' object has no attribute 'status_code'

The problem is, ClientResponse class from aiohttp doesn't have a status_code property. The property that gets a status code is simply called status.

But that's not the whole issue. After I fixed that property name, the client started to generate DocumentError as expected but there's a question mark instead of any messages sent by server:

jsonapi_client.exceptions.DocumentError: Error 404: ?

The reason of this behaviour is that json() method of the ClientResponse is actually a coroutine (that should be awaited somewhere) so error_from_response function gets a generator instead of a proper json collection and fails to get any data from it.

UPD: error_from_response() is also improperly called in http_request_async method.

Some versions info:

  • ArchLinux with 4.19.12 kernel
  • Python 3.7.2
  • jsonapi-client 0.9.6 from PyPI

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions