-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from 2 commits
19e4a35
6b811c0
83e272f
9318bb3
43161eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 | ||
|
||
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)) | ||
|
||
|
||
@pytest.mark.usefixtures('mocked_response') | ||
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 | ||
|
||
|
||
@pytest.mark.usefixtures('mocked_response') | ||
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 |
Uh oh!
There was an error while loading. Please reload this page.