apify-client 3.0.0-beta.5 → 3.0.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/apify_api_error.d.ts +59 -3
- package/dist/apify_api_error.js +66 -2
- package/dist/bundle.js +11 -11
- package/dist/bundle.js.map +1 -1
- package/dist/http_client.js +1 -1
- package/dist/models.d.ts +6 -0
- package/dist/resource_clients/actor_collection.d.ts +0 -2
- package/dist/resource_clients/actor_env_var_collection.d.ts +8 -17
- package/dist/resource_clients/actor_env_var_collection.js +7 -8
- package/dist/resource_clients/actor_version_collection.d.ts +8 -16
- package/dist/resource_clients/actor_version_collection.js +7 -8
- package/dist/resource_clients/request_queue.d.ts +0 -7
- package/dist/resource_clients/request_queue.js +6 -15
- package/dist/utils.d.ts +2 -11
- package/dist/utils.js +4 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,7 +47,9 @@ Besides greatly simplifying the process of querying the Apify API, the client pr
|
|
|
47
47
|
Based on the endpoint, the client automatically extracts the relevant data and returns it in the
|
|
48
48
|
expected format. Date strings are automatically converted to `Date` objects. For exceptions,
|
|
49
49
|
we throw an `ApifyApiError`, which wraps the plain JSON errors returned by API and enriches
|
|
50
|
-
them with other context for easier debugging.
|
|
50
|
+
them with other context for easier debugging. The error is an instance of the subclass matching the
|
|
51
|
+
HTTP status code, such as `NotFoundError` or `RateLimitError`, so a `catch` block can tell them apart
|
|
52
|
+
with `instanceof`.
|
|
51
53
|
|
|
52
54
|
### Retries with exponential backoff
|
|
53
55
|
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import type { AxiosResponse } from 'axios';
|
|
2
|
+
import type { LiteralUnion } from 'type-fest';
|
|
3
|
+
import type { ApifyApiErrorType } from './models.js';
|
|
4
|
+
export type { ApifyApiErrorType } from './models.js';
|
|
2
5
|
/**
|
|
3
6
|
* An `ApifyApiError` is thrown for successful HTTP requests that reach the API,
|
|
4
7
|
* but the API responds with an error response. Typically, those are rate limit
|
|
5
8
|
* errors and internal errors, which are automatically retried, or validation
|
|
6
9
|
* errors, which are thrown immediately, because a correction by the user is
|
|
7
10
|
* needed.
|
|
11
|
+
*
|
|
12
|
+
* The thrown error is an instance of the subclass matching the HTTP status code of the response:
|
|
13
|
+
* {@link InvalidRequestError} (400), {@link UnauthorizedError} (401), {@link ForbiddenError} (403),
|
|
14
|
+
* {@link NotFoundError} (404), {@link ConflictError} (409), {@link RateLimitError} (429) or
|
|
15
|
+
* {@link ServerError} (5xx). Any other status code is thrown as a plain `ApifyApiError`. Every
|
|
16
|
+
* subclass extends `ApifyApiError`, so `instanceof ApifyApiError` matches all of them. Errors that
|
|
17
|
+
* share a status code are told apart by their `type`.
|
|
8
18
|
*/
|
|
9
19
|
export declare class ApifyApiError extends Error {
|
|
10
20
|
name: string;
|
|
@@ -18,9 +28,10 @@ export declare class ApifyApiError extends Error {
|
|
|
18
28
|
*/
|
|
19
29
|
statusCode: number;
|
|
20
30
|
/**
|
|
21
|
-
* The type of the error, as returned by the API.
|
|
31
|
+
* The type of the error, as returned by the API. Typed as the known {@link ApifyApiErrorType}
|
|
32
|
+
* values for autocompletion, while still accepting any string the API may return.
|
|
22
33
|
*/
|
|
23
|
-
type?: string
|
|
34
|
+
type?: LiteralUnion<ApifyApiErrorType, string>;
|
|
24
35
|
/**
|
|
25
36
|
* Number of the API call attempt.
|
|
26
37
|
*/
|
|
@@ -46,6 +57,11 @@ export declare class ApifyApiError extends Error {
|
|
|
46
57
|
* @hidden
|
|
47
58
|
*/
|
|
48
59
|
constructor(response: AxiosResponse, attempt: number);
|
|
60
|
+
/**
|
|
61
|
+
* Creates the error for a failed response as an instance of the subclass matching its HTTP status code.
|
|
62
|
+
* @hidden
|
|
63
|
+
*/
|
|
64
|
+
static fromResponse(response: AxiosResponse, attempt: number): ApifyApiError;
|
|
49
65
|
private _safelyParsePathFromResponse;
|
|
50
66
|
private _extractClientAndMethodFromStack;
|
|
51
67
|
/**
|
|
@@ -54,7 +70,7 @@ export declare class ApifyApiError extends Error {
|
|
|
54
70
|
*
|
|
55
71
|
* Example:
|
|
56
72
|
*
|
|
57
|
-
*
|
|
73
|
+
* NotFoundError: Actor task was not found
|
|
58
74
|
* clientMethod: TaskClient.start
|
|
59
75
|
* statusCode: 404
|
|
60
76
|
* type: record-not-found
|
|
@@ -64,3 +80,43 @@ export declare class ApifyApiError extends Error {
|
|
|
64
80
|
*/
|
|
65
81
|
private _createApiStack;
|
|
66
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Thrown when the Apify API responds with HTTP 400 Bad Request, typically because the request
|
|
85
|
+
* failed validation.
|
|
86
|
+
*/
|
|
87
|
+
export declare class InvalidRequestError extends ApifyApiError {
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Thrown when the Apify API responds with HTTP 401 Unauthorized, because the token is missing
|
|
91
|
+
* or invalid.
|
|
92
|
+
*/
|
|
93
|
+
export declare class UnauthorizedError extends ApifyApiError {
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Thrown when the Apify API responds with HTTP 403 Forbidden, because the token lacks the
|
|
97
|
+
* permission for the operation.
|
|
98
|
+
*/
|
|
99
|
+
export declare class ForbiddenError extends ApifyApiError {
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Thrown when the Apify API responds with HTTP 404 Not Found.
|
|
103
|
+
*/
|
|
104
|
+
export declare class NotFoundError extends ApifyApiError {
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Thrown when the Apify API responds with HTTP 409 Conflict.
|
|
108
|
+
*/
|
|
109
|
+
export declare class ConflictError extends ApifyApiError {
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Thrown when the Apify API responds with HTTP 429 Too Many Requests. The client retries such
|
|
113
|
+
* requests, so the error surfaces once the retries are exhausted.
|
|
114
|
+
*/
|
|
115
|
+
export declare class RateLimitError extends ApifyApiError {
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Thrown when the Apify API responds with an HTTP 5xx status. The client retries such requests,
|
|
119
|
+
* so the error surfaces once the retries are exhausted.
|
|
120
|
+
*/
|
|
121
|
+
export declare class ServerError extends ApifyApiError {
|
|
122
|
+
}
|
package/dist/apify_api_error.js
CHANGED
|
@@ -16,6 +16,13 @@ const CLIENT_METHOD_REGEX = /at( async)? ([A-Za-z]+(Collection)?Client)\._?([A-Z
|
|
|
16
16
|
* errors and internal errors, which are automatically retried, or validation
|
|
17
17
|
* errors, which are thrown immediately, because a correction by the user is
|
|
18
18
|
* needed.
|
|
19
|
+
*
|
|
20
|
+
* The thrown error is an instance of the subclass matching the HTTP status code of the response:
|
|
21
|
+
* {@link InvalidRequestError} (400), {@link UnauthorizedError} (401), {@link ForbiddenError} (403),
|
|
22
|
+
* {@link NotFoundError} (404), {@link ConflictError} (409), {@link RateLimitError} (429) or
|
|
23
|
+
* {@link ServerError} (5xx). Any other status code is thrown as a plain `ApifyApiError`. Every
|
|
24
|
+
* subclass extends `ApifyApiError`, so `instanceof ApifyApiError` matches all of them. Errors that
|
|
25
|
+
* share a status code are told apart by their `type`.
|
|
19
26
|
*/
|
|
20
27
|
export class ApifyApiError extends Error {
|
|
21
28
|
name;
|
|
@@ -29,7 +36,8 @@ export class ApifyApiError extends Error {
|
|
|
29
36
|
*/
|
|
30
37
|
statusCode;
|
|
31
38
|
/**
|
|
32
|
-
* The type of the error, as returned by the API.
|
|
39
|
+
* The type of the error, as returned by the API. Typed as the known {@link ApifyApiErrorType}
|
|
40
|
+
* values for autocompletion, while still accepting any string the API may return.
|
|
33
41
|
*/
|
|
34
42
|
type;
|
|
35
43
|
/**
|
|
@@ -100,6 +108,14 @@ export class ApifyApiError extends Error {
|
|
|
100
108
|
this.stack = this._createApiStack();
|
|
101
109
|
this.data = errorData;
|
|
102
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Creates the error for a failed response as an instance of the subclass matching its HTTP status code.
|
|
113
|
+
* @hidden
|
|
114
|
+
*/
|
|
115
|
+
static fromResponse(response, attempt) {
|
|
116
|
+
const ErrorClass = ERROR_CLASS_BY_STATUS[response.status] ?? (response.status >= 500 ? ServerError : ApifyApiError);
|
|
117
|
+
return new ErrorClass(response, attempt);
|
|
118
|
+
}
|
|
103
119
|
_safelyParsePathFromResponse(response) {
|
|
104
120
|
const urlString = response.config?.url;
|
|
105
121
|
let url;
|
|
@@ -123,7 +139,7 @@ export class ApifyApiError extends Error {
|
|
|
123
139
|
*
|
|
124
140
|
* Example:
|
|
125
141
|
*
|
|
126
|
-
*
|
|
142
|
+
* NotFoundError: Actor task was not found
|
|
127
143
|
* clientMethod: TaskClient.start
|
|
128
144
|
* statusCode: 404
|
|
129
145
|
* type: record-not-found
|
|
@@ -145,3 +161,51 @@ export class ApifyApiError extends Error {
|
|
|
145
161
|
return `${name}: ${this.message}\n${stack}`;
|
|
146
162
|
}
|
|
147
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* Thrown when the Apify API responds with HTTP 400 Bad Request, typically because the request
|
|
166
|
+
* failed validation.
|
|
167
|
+
*/
|
|
168
|
+
export class InvalidRequestError extends ApifyApiError {
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Thrown when the Apify API responds with HTTP 401 Unauthorized, because the token is missing
|
|
172
|
+
* or invalid.
|
|
173
|
+
*/
|
|
174
|
+
export class UnauthorizedError extends ApifyApiError {
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Thrown when the Apify API responds with HTTP 403 Forbidden, because the token lacks the
|
|
178
|
+
* permission for the operation.
|
|
179
|
+
*/
|
|
180
|
+
export class ForbiddenError extends ApifyApiError {
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Thrown when the Apify API responds with HTTP 404 Not Found.
|
|
184
|
+
*/
|
|
185
|
+
export class NotFoundError extends ApifyApiError {
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Thrown when the Apify API responds with HTTP 409 Conflict.
|
|
189
|
+
*/
|
|
190
|
+
export class ConflictError extends ApifyApiError {
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Thrown when the Apify API responds with HTTP 429 Too Many Requests. The client retries such
|
|
194
|
+
* requests, so the error surfaces once the retries are exhausted.
|
|
195
|
+
*/
|
|
196
|
+
export class RateLimitError extends ApifyApiError {
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Thrown when the Apify API responds with an HTTP 5xx status. The client retries such requests,
|
|
200
|
+
* so the error surfaces once the retries are exhausted.
|
|
201
|
+
*/
|
|
202
|
+
export class ServerError extends ApifyApiError {
|
|
203
|
+
}
|
|
204
|
+
const ERROR_CLASS_BY_STATUS = {
|
|
205
|
+
400: InvalidRequestError,
|
|
206
|
+
401: UnauthorizedError,
|
|
207
|
+
403: ForbiddenError,
|
|
208
|
+
404: NotFoundError,
|
|
209
|
+
409: ConflictError,
|
|
210
|
+
429: RateLimitError,
|
|
211
|
+
};
|