Skip to content

Commit

Permalink
Added more details to ApifyClientError in API calls for easier debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
jancurn committed Feb 14, 2019
1 parent ebaac1c commit 2f83ce6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
xxx
==================
- Added more details to `ApifyClientError` for easier debugging

0.5.5 / 2019/01/24
==================
- Improve `.toString()` message of `ApifyClientError`.
Expand Down
8 changes: 4 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export const safeJsonParse = (str) => {
*
* { type: 'ITEM_NOT_FOUND', message: 'Requested item was not found.' }
*
* then uses it's error type or message or both.
* then uses its error type or message or both.
*/
export const newApifyClientErrorFromResponse = (statusCode, body, isApiV1) => {
export const newApifyClientErrorFromResponse = (body, isApiV1, details) => {
const REQUEST_FAILED_ERROR_TYPE = isApiV1 ? REQUEST_FAILED_ERROR_TYPE_V1 : REQUEST_FAILED_ERROR_TYPE_V2;
let parsedBody = {};

Expand All @@ -61,7 +61,7 @@ export const newApifyClientErrorFromResponse = (statusCode, body, isApiV1) => {
const type = error.type || REQUEST_FAILED_ERROR_TYPE;
const message = error.message || REQUEST_FAILED_ERROR_MESSAGE;

return new ApifyClientError(type, message, { statusCode });
return new ApifyClientError(type, message, details);
};

/**
Expand Down Expand Up @@ -132,7 +132,7 @@ export const requestPromise = async (options, stats) => {
// For status codes 300-499 except RATE_LIMIT_EXCEEDED_STATUS_CODE we immediately rejects the promise
// since it's probably caused by invalid url (redirect 3xx) or invalid user input (4xx).
if (statusCode >= 300 && statusCode < 500 && statusCode !== RATE_LIMIT_EXCEEDED_STATUS_CODE) {
throw newApifyClientErrorFromResponse(statusCode, response.body, isApiV1);
throw newApifyClientErrorFromResponse(response.body, isApiV1, { statusCode, url: options.url, method: options.method });
}

const errorDetails = Object.assign(_.pick(options, 'url', 'method', 'qs'), {
Expand Down
20 changes: 11 additions & 9 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,58 @@ describe('utils.safeJsonParse()', () => {

describe('utils.newApifyClientErrorFromResponse()', () => {
it('works with body as object', () => {
const error = utils.newApifyClientErrorFromResponse(404, { type: 'SOME_TYPE', message: 'Some message.' });
expect(error.details.statusCode).to.be.eql(404);
const details = { statusCode: 404 };
const error = utils.newApifyClientErrorFromResponse({ type: 'SOME_TYPE', message: 'Some message.' }, false, details);
expect(error.details).to.be.eql(details);
expect(error.type).to.be.eql('SOME_TYPE');
expect(error.message).to.be.eql('Some message.');
});

it('works with body as JSON string', () => {
const error = utils.newApifyClientErrorFromResponse(404, JSON.stringify({ type: 'SOME_TYPE', message: 'Some message.' }));
const error = utils.newApifyClientErrorFromResponse(
JSON.stringify({ type: 'SOME_TYPE', message: 'Some message.' }), false, { statusCode: 404 });
expect(error.details.statusCode).to.be.eql(404);
expect(error.type).to.be.eql('SOME_TYPE');
expect(error.message).to.be.eql('Some message.');
});

it('works withhout type and message in body', () => {
const error = utils.newApifyClientErrorFromResponse(404, { foo: 'bar' });
const error = utils.newApifyClientErrorFromResponse({ foo: 'bar' }, false, { statusCode: 404 });
expect(error.details.statusCode).to.be.eql(404);
expect(error.type).to.be.eql(REQUEST_FAILED_ERROR_TYPE_V2);
expect(error.message).to.be.eql(REQUEST_FAILED_ERROR_MESSAGE);
});

it('works withhout type in body', () => {
const error = utils.newApifyClientErrorFromResponse(404, { foo: 'bar', message: 'Some message.' });
const error = utils.newApifyClientErrorFromResponse({ foo: 'bar', message: 'Some message.' }, false, { statusCode: 404 });
expect(error.details.statusCode).to.be.eql(404);
expect(error.type).to.be.eql(REQUEST_FAILED_ERROR_TYPE_V2);
expect(error.message).to.be.eql('Some message.');
});

it('works withhout type and message in body for API V1', () => {
const error = utils.newApifyClientErrorFromResponse(404, { foo: 'bar' }, true);
const error = utils.newApifyClientErrorFromResponse({ foo: 'bar' }, true, { statusCode: 404 });
expect(error.details.statusCode).to.be.eql(404);
expect(error.type).to.be.eql(REQUEST_FAILED_ERROR_TYPE_V1);
expect(error.message).to.be.eql(REQUEST_FAILED_ERROR_MESSAGE);
});

it('works withhout type in body for API V1', () => {
const error = utils.newApifyClientErrorFromResponse(404, { foo: 'bar', message: 'Some message.' }, true);
const error = utils.newApifyClientErrorFromResponse({ foo: 'bar', message: 'Some message.' }, true, { statusCode: 404 });
expect(error.details.statusCode).to.be.eql(404);
expect(error.type).to.be.eql(REQUEST_FAILED_ERROR_TYPE_V1);
expect(error.message).to.be.eql('Some message.');
});

it('works withhout message in body', () => {
const error = utils.newApifyClientErrorFromResponse(404, { foo: 'bar', type: 'SOME_TYPE' });
const error = utils.newApifyClientErrorFromResponse({ foo: 'bar', type: 'SOME_TYPE' }, false, { statusCode: 404 });
expect(error.details.statusCode).to.be.eql(404);
expect(error.type).to.be.eql('SOME_TYPE');
expect(error.message).to.be.eql(REQUEST_FAILED_ERROR_MESSAGE);
});

it('works with error as subobject', () => {
const error = utils.newApifyClientErrorFromResponse(404, { error: { type: 'SOME_TYPE', message: 'Some message.' } });
const error = utils.newApifyClientErrorFromResponse({ error: { type: 'SOME_TYPE', message: 'Some message.' } }, false, { statusCode: 404 });
expect(error.details.statusCode).to.be.eql(404);
expect(error.type).to.be.eql('SOME_TYPE');
expect(error.message).to.be.eql('Some message.');
Expand Down

0 comments on commit 2f83ce6

Please sign in to comment.