Skip to content
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
10 changes: 5 additions & 5 deletions src/apify_client/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ class ApifyApiError(ApifyClientError):

def __new__(cls, response: HttpResponse, attempt: int, *, method: str = 'GET') -> Self: # noqa: ARG004
"""Dispatch to the subclass matching the response's HTTP status code, if any."""
instance = super().__new__(cls)
if cls is ApifyApiError:
status = response.status_code
mapped = _STATUS_TO_CLASS.get(status)
if mapped is None and status >= HTTPStatus.INTERNAL_SERVER_ERROR:
mapped = ServerError
if mapped is not None:
# Retagging is runtime-equivalent to allocating `mapped` directly, but keeps the return type `Self`.
instance.__class__ = mapped
return instance
# Every mapped class subclasses `ApifyApiError`, so the `issubclass` check always holds; it narrows `mapped`
# to `type[Self]` for the return type.
if mapped is not None and issubclass(mapped, cls):
return super().__new__(mapped)
return super().__new__(cls)

def __init__(self, response: HttpResponse, attempt: int, *, method: str = 'GET') -> None:
"""Initialize the API error from a failed response.
Expand Down
Loading
Loading