diff --git a/CHANGELOG.md b/CHANGELOG.md index aedc5011a..e505800b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/clients/http_client/http_client.ts b/src/clients/http_client/http_client.ts index 0d4810c6c..e36c1d975 100644 --- a/src/clients/http_client/http_client.ts +++ b/src/clients/http_client/http_client.ts @@ -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( @@ -234,7 +234,7 @@ class HttpClient { } const errorMessage = errorMessages.length - ? `: ${errorMessages.join('. ')}` + ? `:\n${errorMessages.join('\n')}` : ''; switch (true) { case response.status === StatusCode.TooManyRequests: { diff --git a/src/clients/http_client/test/http_client.test.ts b/src/clients/http_client/test/http_client.test.ts index f72af80c9..760456faf 100644 --- a/src/clients/http_client/test/http_client.test.ts +++ b/src/clients/http_client/test/http_client.test.ts @@ -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) {