Skip to content

Commit 3b7c4b7

Browse files
gippyJaroslav Hejlek
andauthored
feat: added data property to API error object (#559)
This update is needed because in API we will be adding a new data field to the API error, which will be used during dataset validation. apify/apify-docs#1079 --------- Co-authored-by: Jaroslav Hejlek <[email protected]>
1 parent 5480321 commit 3b7c4b7

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

src/apify_api_error.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@ export class ApifyApiError extends Error {
6161
*/
6262
originalStack: string;
6363

64+
/**
65+
* Additional data provided by the API about the error
66+
*/
67+
data?: Record<string, unknown>;
68+
6469
/**
6570
* @hidden
6671
*/
6772
constructor(response: AxiosResponse, attempt: number) {
6873
let message!: string;
6974
let type: string | undefined;
7075
let responseData = response.data;
76+
let errorData: Record<string, unknown> | undefined;
7177

7278
// Some methods (e.g. downloadItems) set up forceBuffer on request response. If this request failed
7379
// the body buffer needs to parse to get the correct error.
@@ -83,6 +89,7 @@ export class ApifyApiError extends Error {
8389
const { error } = responseData;
8490
message = error.message;
8591
type = error.type;
92+
errorData = error.data;
8693
} else if (responseData) {
8794
let dataString;
8895
try {
@@ -106,6 +113,8 @@ export class ApifyApiError extends Error {
106113

107114
this.originalStack = stack.slice(stack.indexOf('\n'));
108115
this.stack = this._createApiStack();
116+
117+
this.data = errorData;
109118
}
110119

111120
private _safelyParsePathFromResponse(response: AxiosResponse) {

test/apify_api_error.test.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
const { Browser } = require('./_helper');
1+
const { Browser, DEFAULT_OPTIONS } = require('./_helper');
2+
const mockServer = require('./mock_server/server');
23
const { ApifyClient } = require('../src/index');
34

45
describe('ApifyApiError', () => {
6+
let baseUrl;
57
const browser = new Browser();
68

79
beforeAll(async () => {
810
await browser.start();
11+
const server = await mockServer.start();
12+
baseUrl = `http://localhost:${server.address().port}`;
913
});
1014

1115
afterAll(async () => {
12-
await browser.cleanUpBrowser();
16+
await Promise.all([
17+
mockServer.close(),
18+
browser.cleanUpBrowser(),
19+
]);
1320
});
1421

1522
test('should carry all the information', async () => {
@@ -62,4 +69,54 @@ describe('ApifyApiError', () => {
6269
expect(error.httpMethod).toEqual('get');
6370
expect(error.attempt).toEqual(1);
6471
});
72+
73+
test('should carry additional error data if provided', async () => {
74+
const datasetId = '400'; // check add_routes.js to see details of this mock
75+
const data = JSON.stringify([{ someData: 'someValue' }, { someData: 'someValue' }]);
76+
const clientConfig = {
77+
baseUrl,
78+
maxRetries: 0,
79+
...DEFAULT_OPTIONS,
80+
};
81+
82+
// Works in node
83+
try {
84+
const client = new ApifyClient(clientConfig);
85+
await client.dataset(datasetId).pushItems(data);
86+
throw new Error('wrong error');
87+
} catch (err) {
88+
expect(err.name).toEqual('ApifyApiError');
89+
expect(err.type).toEqual('schema-validation-error');
90+
expect(err.data).toEqual({
91+
invalidItems: {
92+
0: [`should have required property 'name'`],
93+
},
94+
});
95+
}
96+
97+
// Works in browser
98+
const page = await browser.getInjectedPage();
99+
const error = await page.evaluate(async (cConfig, dId, d) => {
100+
const client = new window.Apify.ApifyClient(cConfig);
101+
const datasetClient = client.dataset(dId);
102+
try {
103+
await datasetClient.pushItems(d);
104+
throw new Error('wrong error');
105+
} catch (err) {
106+
const serializableErr = {};
107+
Object.getOwnPropertyNames(err).forEach((prop) => {
108+
serializableErr[prop] = err[prop];
109+
});
110+
serializableErr.resourcePath = datasetClient.resourcePath;
111+
return serializableErr;
112+
}
113+
}, clientConfig, datasetId, data);
114+
expect(error.name).toEqual('ApifyApiError');
115+
expect(error.type).toEqual('schema-validation-error');
116+
expect(error.data).toEqual({
117+
invalidItems: {
118+
0: [`should have required property 'name'`],
119+
},
120+
});
121+
});
65122
});

test/mock_server/routes/add_routes.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,20 @@ const HANDLERS = {
4040
let payload = {};
4141
if (responseStatusCode === 200) payload = { data: { id } };
4242
else if (responseStatusCode === 204) payload = null;
43-
else if (responseStatusCode === 404) {
43+
else if (responseStatusCode === 400) {
44+
// This is not ideal, what if we have more endpoints which can return 400?
45+
payload = {
46+
error: {
47+
type: 'schema-validation-error',
48+
message: 'Schema validation failed',
49+
data: {
50+
invalidItems: {
51+
0: [`should have required property 'name'`],
52+
},
53+
},
54+
},
55+
};
56+
} else if (responseStatusCode === 404) {
4457
payload = {
4558
error: {
4659
type: 'record-not-found',

0 commit comments

Comments
 (0)