Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Expand objects in HTTP request error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Oct 22, 2021
1 parent 249896d commit f724478
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

### Fixed

- Fixed the HTTP client error messages to expand objects [#252](https://github.com/Shopify/shopify-node-api/pull/252)

## [1.4.2] - 2021-10-20

- Added `October21` to `ApiVersion` [#247](https://github.com/Shopify/shopify-node-api/pull/247)
Expand Down
4 changes: 2 additions & 2 deletions src/clients/http_client/http_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class HttpClient {
} else {
const errorMessages: string[] = [];
if (body.errors) {
errorMessages.push(body.errors);
errorMessages.push(JSON.stringify(body.errors, null, 2));
}
if (response.headers && response.headers.get('x-request-id')) {
errorMessages.push(
Expand All @@ -234,7 +234,7 @@ class HttpClient {
}

const errorMessage = errorMessages.length
? `: ${errorMessages.join('. ')}`
? `:\n${errorMessages.join('\n')}`
: '';
switch (true) {
case response.status === StatusCode.TooManyRequests: {
Expand Down
51 changes: 51 additions & 0 deletions src/clients/http_client/test/http_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,57 @@ describe('HTTP client', () => {
);
expect(fileContent).toContain(`Stack Trace: Error:`);
});

it('properly encodes strings in the error message', async () => {
setRestClientRetryTime(0);
const client = new HttpClient(domain);

fetchMock.mockResponses(
[
JSON.stringify({errors: 'Something went wrong'}),
{status: 500, statusText: 'Did not work'},
],
);

let caught = false;
await client.get({path: '/url/path'}).then(() => fail('Expected request to fail')).catch((error) => {
caught = true;
expect(error).toBeInstanceOf(ShopifyErrors.HttpInternalError);
expect(error.message).toEqual(
`Shopify internal error:` +
`\n"Something went wrong"`,
);
});
expect(caught).toEqual(true);
assertHttpRequest({method: 'GET', domain, path: '/url/path'});
});

it('properly encodes objects in the error message', async () => {
setRestClientRetryTime(0);
const client = new HttpClient(domain);

fetchMock.mockResponses(
[
JSON.stringify({errors: {title: 'Invalid title', description: 'Invalid description'}}),
{status: 500, statusText: 'Did not work'},
],
);

let caught = false;
await client.get({path: '/url/path'}).then(() => fail('Expected request to fail')).catch((error) => {
caught = true;
expect(error).toBeInstanceOf(ShopifyErrors.HttpInternalError);
expect(error.message).toEqual(
`Shopify internal error:` +
`\n{` +
`\n "title": "Invalid title",` +
`\n "description": "Invalid description"` +
`\n}`,
);
});
expect(caught).toEqual(true);
assertHttpRequest({method: 'GET', domain, path: '/url/path'});
});
});

function setRestClientRetryTime(time: number) {
Expand Down

0 comments on commit f724478

Please sign in to comment.