Skip to content

feat: added data property to API error object #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2024
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
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