You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/02_concepts/13_exceptions.mdx
+19-15Lines changed: 19 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,24 +13,24 @@ When you run an Actor, exceptions come from a few layers: the Apify API client f
13
13
14
14
## Errors from the Apify API
15
15
16
-
Every SDK operation that talks to the Apify API can raise `ApifyApiError`. This includes <ApiLinkto="class/Actor#start">`Actor.start`</ApiLink>, <ApiLinkto="class/Actor#call">`Actor.call`</ApiLink>, `Actor.abort`, `Actor.metamorph`, `Actor.add_webhook`, charging, and all storage operations on datasets, key-value stores, and request queues. The SDK raises these client exceptions as-is, so you keep the HTTP status code, the error type, and the response data on the exception.
16
+
Every SDK operation that talks to the Apify API can raise <ApiLinkto="class/ApifyApiError">`ApifyApiError`</ApiLink>. This includes <ApiLinkto="class/Actor#start">`Actor.start`</ApiLink>, <ApiLinkto="class/Actor#call">`Actor.call`</ApiLink>, <ApiLinkto="class/Actor#abort">`Actor.abort`</ApiLink>, <ApiLinkto="class/Actor#metamorph">`Actor.metamorph`</ApiLink>, <ApiLinkto="class/Actor#add_webhook">`Actor.add_webhook`</ApiLink>, charging, and all storage operations on datasets, key-value stores, and request queues. The SDK raises these client exceptions as-is, so you keep the HTTP status code, the error type, and the response data on the exception.
17
17
18
-
`ApifyApiError` dispatches to a subclass based on the HTTP status code:
18
+
<ApiLinkto="class/ApifyApiError">`ApifyApiError`</ApiLink> dispatches to a subclass based on the HTTP status code:
19
19
20
-
-`UnauthorizedError` (401) and `ForbiddenError` (403) for an unauthorized or forbidden request.
21
-
-`NotFoundError` (404) when the Actor, run, or storage does not exist.
22
-
-`ConflictError` (409) for a conflicting request.
23
-
-`RateLimitError` (429) when the API rate limit is hit.
24
-
-`ServerError` for any 5xx response.
25
-
-`InvalidRequestError` (400) when the API rejects the request as malformed.
20
+
-<ApiLinkto="class/UnauthorizedError">`UnauthorizedError`</ApiLink> (401) and <ApiLinkto="class/ForbiddenError">`ForbiddenError`</ApiLink> (403) for an unauthorized or forbidden request.
21
+
-<ApiLinkto="class/NotFoundError">`NotFoundError`</ApiLink> (404) when the Actor, run, or storage does not exist.
22
+
-<ApiLinkto="class/ConflictError">`ConflictError`</ApiLink> (409) for a conflicting request.
23
+
-<ApiLinkto="class/RateLimitError">`RateLimitError`</ApiLink> (429) when the API rate limit is hit.
24
+
-<ApiLinkto="class/ServerError">`ServerError`</ApiLink> for any 5xx response.
25
+
-<ApiLinkto="class/InvalidRequestError">`InvalidRequestError`</ApiLink> (400) when the API rejects the request as malformed.
26
26
27
-
The client retries rate-limited and server errors on its own, so you only see `RateLimitError` or `ServerError` once those retries are exhausted. The `apify.errors` module re-exports the whole client error hierarchy, so you can import everything from one place:
27
+
The client retries rate-limited and server errors on its own, so you only see <ApiLinkto="class/RateLimitError">`RateLimitError`</ApiLink> or <ApiLinkto="class/ServerError">`ServerError`</ApiLink> once those retries are exhausted. The `apify.errors` module re-exports the whole client error hierarchy, so you can import everything from one place:
28
28
29
29
```python
30
30
from apify.errors import ApifyApiError, NotFoundError, RateLimitError
31
31
```
32
32
33
-
Catch `ApifyApiError` to handle any API failure in one place, then branch on the subclass or the HTTP `status_code`. To react to a specific failure, catch its subclass first:
33
+
Catch <ApiLinkto="class/ApifyApiError">`ApifyApiError`</ApiLink> to handle any API failure in one place, then branch on the subclass or the HTTP `status_code`. To react to a specific failure, catch its subclass first:
@@ -40,10 +40,10 @@ Catch `ApifyApiError` to handle any API failure in one place, then branch on the
40
40
41
41
The SDK raises standard Python exceptions when it is used incorrectly or given invalid input. These point to a bug or a bad argument in your code, so the fix is to correct the call rather than to catch the exception.
42
42
43
-
-`RuntimeError` when an `Actor` method is used outside the `async with Actor:` block, either before initialization or after exit, or when the Actor is initialized twice.
44
-
-`ValueError` for an invalid argument, such as a malformed `timeout`, an invalid proxy configuration, charging an automatically charged event by hand, or pushing data that is not JSON-serializable or is over the size limit.
45
-
-`TypeError` for an argument of the wrong type.
46
-
-`ConnectionError` when <ApiLinkto="class/Actor#create_proxy_configuration">`Actor.create_proxy_configuration`</ApiLink> verifies Apify Proxy access and the proxy reports that you have none.
43
+
-[`RuntimeError`](https://docs.python.org/3/library/exceptions.html#RuntimeError) when an `Actor` method is used outside the `async with Actor:` block, either before initialization or after exit, or when the Actor is initialized twice.
44
+
-[`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError) for an invalid argument, such as a malformed `timeout`, an invalid proxy configuration, charging an automatically charged event by hand, or pushing data that is not JSON-serializable or is over the size limit.
45
+
-[`TypeError`](https://docs.python.org/3/library/exceptions.html#TypeError) for an argument of the wrong type.
46
+
-[`ConnectionError`](https://docs.python.org/3/library/exceptions.html#ConnectionError) when <ApiLinkto="class/Actor#create_proxy_configuration">`Actor.create_proxy_configuration`</ApiLink> verifies Apify Proxy access and the proxy reports that you have none.
47
47
48
48
## Run failures
49
49
@@ -59,4 +59,8 @@ If your Actor runs a [Crawlee](https://crawlee.dev/python) crawler, failures ins
59
59
60
60
## The pay-per-event charge limit
61
61
62
-
Reaching the pay-per-event charge limit does not raise an error. The SDK caps charging and data pushing instead, and your Actor keeps running. To detect the limit, check the `event_charge_limit_reached` field on the `ChargeResult` returned by <ApiLinkto="class/Actor#charge">`Actor.charge`</ApiLink> or `Actor.push_data`. For details, see [Pay-per-event monetization](./pay-per-event).
62
+
Reaching the pay-per-event charge limit does not raise an error. The SDK caps charging and data pushing instead, and your Actor keeps running. To detect the limit, check the `event_charge_limit_reached` field on the `ChargeResult` returned by <ApiLinkto="class/Actor#charge">`Actor.charge`</ApiLink> or <ApiLinkto="class/Actor#push_data">`Actor.push_data`</ApiLink>. For details, see [Pay-per-event monetization](./pay-per-event).
63
+
64
+
## Conclusion
65
+
66
+
Most failures you handle at runtime are <ApiLinkto="class/ApifyApiError">`ApifyApiError`</ApiLink> from the API client. Catch it to cover any API failure, and reach for a subclass or the HTTP `status_code` when you need finer control. The standard [`RuntimeError`](https://docs.python.org/3/library/exceptions.html#RuntimeError), [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError), and [`TypeError`](https://docs.python.org/3/library/exceptions.html#TypeError) signal a bug or bad input, so correct the call rather than catch them. After <ApiLinkto="class/Actor#call">`Actor.call`</ApiLink>, check `run.status` to react to a failed run, and let Crawlee handle the errors raised inside a crawler.
0 commit comments