|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import UTC, datetime |
| 4 | +from typing import Any, cast |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from apify_client._models import Run |
| 9 | +from apify_client.errors import ( |
| 10 | + ApifyApiError, |
| 11 | + ConflictError, |
| 12 | + ForbiddenError, |
| 13 | + InvalidRequestError, |
| 14 | + NotFoundError, |
| 15 | + ServerError, |
| 16 | + UnauthorizedError, |
| 17 | +) |
| 18 | +from apify_client.errors import RateLimitError as ClientRateLimitError |
| 19 | + |
| 20 | +import apify |
| 21 | +from apify.errors import ( |
| 22 | + ActorAuthenticationError, |
| 23 | + ActorChargeLimitExceededError, |
| 24 | + ActorError, |
| 25 | + ActorInputValidationError, |
| 26 | + ActorRateLimitError, |
| 27 | + ActorRunError, |
| 28 | + ActorTimeoutError, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +class _FakeResponse: |
| 33 | + """Minimal stand-in for `apify_client`'s HTTP response, enough to build its API errors.""" |
| 34 | + |
| 35 | + def __init__(self, status_code: int) -> None: |
| 36 | + self.status_code = status_code |
| 37 | + self.text = 'error text' |
| 38 | + |
| 39 | + def json(self) -> dict[str, Any]: |
| 40 | + return {'error': {'message': 'boom', 'type': 'some-error-type'}} |
| 41 | + |
| 42 | + |
| 43 | +def _client_error(error_cls: type[ApifyApiError], status_code: int) -> ApifyApiError: |
| 44 | + return error_cls(cast('Any', _FakeResponse(status_code)), 1) |
| 45 | + |
| 46 | + |
| 47 | +def _make_run(*, status: str, exit_code: int | None = None, status_message: str | None = None) -> Run: |
| 48 | + return Run.model_validate( |
| 49 | + { |
| 50 | + 'id': 'run123', |
| 51 | + 'actId': 'act123', |
| 52 | + 'userId': 'user123', |
| 53 | + 'startedAt': datetime.now(UTC).isoformat(), |
| 54 | + 'status': status, |
| 55 | + 'statusMessage': status_message, |
| 56 | + 'exitCode': exit_code, |
| 57 | + 'meta': {'origin': 'DEVELOPMENT'}, |
| 58 | + 'buildId': 'build123', |
| 59 | + 'defaultDatasetId': 'ds123', |
| 60 | + 'defaultKeyValueStoreId': 'kvs123', |
| 61 | + 'defaultRequestQueueId': 'rq123', |
| 62 | + 'containerUrl': 'https://container', |
| 63 | + 'buildNumber': '0.0.1', |
| 64 | + 'generalAccess': 'RESTRICTED', |
| 65 | + 'stats': {'restartCount': 0, 'resurrectCount': 0, 'computeUnits': 1}, |
| 66 | + 'options': {'build': 'latest', 'timeoutSecs': 4, 'memoryMbytes': 1024, 'diskMbytes': 1024}, |
| 67 | + } |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def test_actor_error_defaults() -> None: |
| 72 | + error = ActorError('something went wrong') |
| 73 | + assert error.code == 'apify-error' |
| 74 | + assert error.retryable is False |
| 75 | + assert str(error) == 'something went wrong' |
| 76 | + |
| 77 | + |
| 78 | +def test_actor_error_overrides_are_instance_scoped() -> None: |
| 79 | + error = ActorError('boom', code='custom', retryable=True) |
| 80 | + assert error.code == 'custom' |
| 81 | + assert error.retryable is True |
| 82 | + # Overriding on an instance must not leak to the class default. |
| 83 | + assert ActorError.code == 'apify-error' |
| 84 | + assert ActorError.retryable is False |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize( |
| 88 | + ('error_cls', 'expected_code', 'expected_retryable'), |
| 89 | + [ |
| 90 | + (ActorRateLimitError, 'rate-limit-exceeded', True), |
| 91 | + (ActorTimeoutError, 'actor-timed-out', True), |
| 92 | + (ActorAuthenticationError, 'authentication-error', False), |
| 93 | + (ActorChargeLimitExceededError, 'charge-limit-exceeded', False), |
| 94 | + (ActorInputValidationError, 'input-validation-error', False), |
| 95 | + (ActorRunError, 'actor-run-failed', False), |
| 96 | + ], |
| 97 | +) |
| 98 | +def test_subclass_codes_and_retryable( |
| 99 | + error_cls: type[ActorError], expected_code: str, *, expected_retryable: bool |
| 100 | +) -> None: |
| 101 | + assert error_cls.code == expected_code |
| 102 | + assert error_cls.retryable is expected_retryable |
| 103 | + assert issubclass(error_cls, ActorError) |
| 104 | + |
| 105 | + |
| 106 | +def test_input_validation_error_is_value_error() -> None: |
| 107 | + """`except ValueError` must still catch `ActorInputValidationError`.""" |
| 108 | + with pytest.raises(ValueError, match='bad input'): |
| 109 | + raise ActorInputValidationError('bad input') |
| 110 | + |
| 111 | + |
| 112 | +def test_actor_timeout_error_is_actor_run_error() -> None: |
| 113 | + assert issubclass(ActorTimeoutError, ActorRunError) |
| 114 | + |
| 115 | + |
| 116 | +def test_actor_run_error_carries_run_metadata() -> None: |
| 117 | + run = _make_run(status='FAILED', exit_code=1, status_message='Actor crashed') |
| 118 | + error = ActorRunError(run) |
| 119 | + assert error.run_id == 'run123' |
| 120 | + assert error.status == 'FAILED' |
| 121 | + assert error.exit_code == 1 |
| 122 | + assert error.status_message == 'Actor crashed' |
| 123 | + assert error.retryable is False |
| 124 | + assert 'run123' in str(error) |
| 125 | + assert 'Actor crashed' in str(error) |
| 126 | + |
| 127 | + |
| 128 | +def test_actor_run_error_from_run_failed() -> None: |
| 129 | + error = ActorRunError.from_run(_make_run(status='FAILED')) |
| 130 | + assert type(error) is ActorRunError |
| 131 | + assert not error.retryable |
| 132 | + |
| 133 | + |
| 134 | +def test_actor_run_error_from_run_timed_out() -> None: |
| 135 | + error = ActorRunError.from_run(_make_run(status='TIMED-OUT')) |
| 136 | + assert isinstance(error, ActorTimeoutError) |
| 137 | + assert error.retryable is True |
| 138 | + assert error.run_id == 'run123' |
| 139 | + assert error.code == 'actor-timed-out' |
| 140 | + |
| 141 | + |
| 142 | +@pytest.mark.parametrize( |
| 143 | + ('client_error', 'expected_cls', 'expected_retryable'), |
| 144 | + [ |
| 145 | + (_client_error(UnauthorizedError, 401), ActorAuthenticationError, False), |
| 146 | + (_client_error(ForbiddenError, 403), ActorAuthenticationError, False), |
| 147 | + (_client_error(ClientRateLimitError, 429), ActorRateLimitError, True), |
| 148 | + (_client_error(ServerError, 500), ActorError, True), |
| 149 | + (_client_error(InvalidRequestError, 400), ActorInputValidationError, False), |
| 150 | + (_client_error(NotFoundError, 404), ActorError, False), |
| 151 | + (_client_error(ConflictError, 409), ActorError, False), |
| 152 | + ], |
| 153 | +) |
| 154 | +def test_from_client_error_mapping( |
| 155 | + client_error: ApifyApiError, |
| 156 | + expected_cls: type[ActorError], |
| 157 | + *, |
| 158 | + expected_retryable: bool, |
| 159 | +) -> None: |
| 160 | + mapped = ActorError.from_client_error(client_error) |
| 161 | + assert type(mapped) is expected_cls |
| 162 | + assert mapped.retryable is expected_retryable |
| 163 | + |
| 164 | + |
| 165 | +def test_from_client_error_unknown_exception_falls_back() -> None: |
| 166 | + mapped = ActorError.from_client_error(RuntimeError('not a client error')) |
| 167 | + assert type(mapped) is ActorError |
| 168 | + assert mapped.retryable is False |
| 169 | + assert 'not a client error' in str(mapped) |
| 170 | + |
| 171 | + |
| 172 | +def test_errors_exported_from_top_level() -> None: |
| 173 | + for name in ( |
| 174 | + 'ActorError', |
| 175 | + 'ActorRunError', |
| 176 | + 'ActorTimeoutError', |
| 177 | + 'ActorAuthenticationError', |
| 178 | + 'ActorChargeLimitExceededError', |
| 179 | + 'ActorInputValidationError', |
| 180 | + 'ActorRateLimitError', |
| 181 | + ): |
| 182 | + assert hasattr(apify, name) |
| 183 | + assert name in apify.__all__ |
| 184 | + assert getattr(apify, name) is getattr(apify.errors, name) |
0 commit comments