Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
61 changes: 38 additions & 23 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ keywords = [
[tool.poetry.dependencies]
python = "^3.9"
apify-shared = ">=1.1.2"
httpx = ">=0.25.0"
# TODO: relax the upper bound once the issue is resolved:
# https://github.com/apify/apify-client-python/issues/313
httpx = "~0.27.0"
more_itertools = ">=10.0.0"

[tool.poetry.group.dev.dependencies]
Expand All @@ -58,6 +60,7 @@ pytest-only = "~2.1.0"
pytest-timeout = "~2.3.0"
pytest-xdist = "~3.6.0"
redbaron = "~0.9.0"
respx = "^0.21.1"
ruff = "~0.8.0"
setuptools = "~75.6.0" # setuptools are used by pytest but not explicitly required

Expand Down
3 changes: 3 additions & 0 deletions src/apify_client/_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ def __init__(self, response: httpx.Response, attempt: int) -> None:
"""
self.message: str | None = None
self.type: str | None = None
self.data = dict[str, str]()

self.message = f'Unexpected error: {response.text}'
try:
response_data = response.json()
if 'error' in response_data:
self.message = response_data['error']['message']
self.type = response_data['error']['type']
if 'data' in response_data['error']:
self.data = response_data['error']['data']
except ValueError:
pass

Expand Down
49 changes: 49 additions & 0 deletions tests/unit/test_client_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json

import httpx
import pytest
import respx
from respx import MockRouter

from apify_client._errors import ApifyApiError
from apify_client._http_client import HTTPClient, HTTPClientAsync

_TEST_URL = 'http://example.com'
_EXPECTED_MESSAGE = 'some_message'
_EXPECTED_TYPE = 'some_type'
_EXPECTED_DATA = {
'invalidItems': {'0': ["should have required property 'name'"], '1': ["should have required property 'name'"]}
}


@respx.mock
@pytest.fixture(autouse=True)
def mocked_response(respx_mock: MockRouter) -> None:
response_content = json.dumps(
{'error': {'message': _EXPECTED_MESSAGE, 'type': _EXPECTED_TYPE, 'data': _EXPECTED_DATA}}
)
respx_mock.get(_TEST_URL).mock(return_value=httpx.Response(400, content=response_content))


def test_client_apify_api_error_with_data() -> None:
"""Test that client correctly throws ApifyApiError with error data from response."""
client = HTTPClient()

with pytest.raises(ApifyApiError) as e:
client.call(method='GET', url=_TEST_URL)

assert e.value.message == _EXPECTED_MESSAGE
assert e.value.type == _EXPECTED_TYPE
assert e.value.data == _EXPECTED_DATA


async def test_async_client_apify_api_error_with_data() -> None:
"""Test that async client correctly throws ApifyApiError with error data from response."""
client = HTTPClientAsync()

with pytest.raises(ApifyApiError) as e:
await client.call(method='GET', url=_TEST_URL)

assert e.value.message == _EXPECTED_MESSAGE
assert e.value.type == _EXPECTED_TYPE
assert e.value.data == _EXPECTED_DATA
Loading