From cddbc806d84afaecf7e62b1f141c3c7e2bdc486f Mon Sep 17 00:00:00 2001 From: Paulo Margarido Date: Fri, 22 Oct 2021 13:19:32 -0400 Subject: [PATCH] Expand objects in HTTP request error messages --- CHANGELOG.md | 2 + src/clients/http_client/http_client.ts | 4 +- .../http_client/test/http_client.test.ts | 51 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aedc5011a..01deabcc6 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 + ## [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) {