Skip to content

Commit

Permalink
feat: added data property to API error object (#559)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
gippy and Jaroslav Hejlek authored Jun 26, 2024
1 parent 5480321 commit 3b7c4b7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/apify_api_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,19 @@ export class ApifyApiError extends Error {
*/
originalStack: string;

/**
* Additional data provided by the API about the error
*/
data?: Record<string, unknown>;

/**
* @hidden
*/
constructor(response: AxiosResponse, attempt: number) {
let message!: string;
let type: string | undefined;
let responseData = response.data;
let errorData: Record<string, unknown> | undefined;

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

this.originalStack = stack.slice(stack.indexOf('\n'));
this.stack = this._createApiStack();

this.data = errorData;
}

private _safelyParsePathFromResponse(response: AxiosResponse) {
Expand Down
61 changes: 59 additions & 2 deletions test/apify_api_error.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const { Browser } = require('./_helper');
const { Browser, DEFAULT_OPTIONS } = require('./_helper');
const mockServer = require('./mock_server/server');
const { ApifyClient } = require('../src/index');

describe('ApifyApiError', () => {
let baseUrl;
const browser = new Browser();

beforeAll(async () => {
await browser.start();
const server = await mockServer.start();
baseUrl = `http://localhost:${server.address().port}`;
});

afterAll(async () => {
await browser.cleanUpBrowser();
await Promise.all([
mockServer.close(),
browser.cleanUpBrowser(),
]);
});

test('should carry all the information', async () => {
Expand Down Expand Up @@ -62,4 +69,54 @@ describe('ApifyApiError', () => {
expect(error.httpMethod).toEqual('get');
expect(error.attempt).toEqual(1);
});

test('should carry additional error data if provided', async () => {
const datasetId = '400'; // check add_routes.js to see details of this mock
const data = JSON.stringify([{ someData: 'someValue' }, { someData: 'someValue' }]);
const clientConfig = {
baseUrl,
maxRetries: 0,
...DEFAULT_OPTIONS,
};

// Works in node
try {
const client = new ApifyClient(clientConfig);
await client.dataset(datasetId).pushItems(data);
throw new Error('wrong error');
} catch (err) {
expect(err.name).toEqual('ApifyApiError');
expect(err.type).toEqual('schema-validation-error');
expect(err.data).toEqual({
invalidItems: {
0: [`should have required property 'name'`],
},
});
}

// Works in browser
const page = await browser.getInjectedPage();
const error = await page.evaluate(async (cConfig, dId, d) => {
const client = new window.Apify.ApifyClient(cConfig);
const datasetClient = client.dataset(dId);
try {
await datasetClient.pushItems(d);
throw new Error('wrong error');
} catch (err) {
const serializableErr = {};
Object.getOwnPropertyNames(err).forEach((prop) => {
serializableErr[prop] = err[prop];
});
serializableErr.resourcePath = datasetClient.resourcePath;
return serializableErr;
}
}, clientConfig, datasetId, data);
expect(error.name).toEqual('ApifyApiError');
expect(error.type).toEqual('schema-validation-error');
expect(error.data).toEqual({
invalidItems: {
0: [`should have required property 'name'`],
},
});
});
});
15 changes: 14 additions & 1 deletion test/mock_server/routes/add_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,20 @@ const HANDLERS = {
let payload = {};
if (responseStatusCode === 200) payload = { data: { id } };
else if (responseStatusCode === 204) payload = null;
else if (responseStatusCode === 404) {
else if (responseStatusCode === 400) {
// This is not ideal, what if we have more endpoints which can return 400?
payload = {
error: {
type: 'schema-validation-error',
message: 'Schema validation failed',
data: {
invalidItems: {
0: [`should have required property 'name'`],
},
},
},
};
} else if (responseStatusCode === 404) {
payload = {
error: {
type: 'record-not-found',
Expand Down

0 comments on commit 3b7c4b7

Please sign in to comment.