Skip to content

feat: Add error data to ApifyApiError #314

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

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
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
67 changes: 41 additions & 26 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.25 <0.28.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