diff --git a/.changeset/friendly-beers-melt.md b/.changeset/friendly-beers-melt.md new file mode 100644 index 000000000..9071ab7bf --- /dev/null +++ b/.changeset/friendly-beers-melt.md @@ -0,0 +1,5 @@ +--- +"@shopify/shopify-api": patch +--- + +Include error message into HttpRequestError diff --git a/packages/shopify-api/lib/clients/admin/__tests__/admin_graphql_client.test.ts b/packages/shopify-api/lib/clients/admin/__tests__/admin_graphql_client.test.ts index 53e520d8d..7b5f7297e 100644 --- a/packages/shopify-api/lib/clients/admin/__tests__/admin_graphql_client.test.ts +++ b/packages/shopify-api/lib/clients/admin/__tests__/admin_graphql_client.test.ts @@ -276,18 +276,19 @@ describe('GraphQL client', () => { } }`; - queueError( - new FetchError( - `uri requested responds with an invalid redirect URL: http://test.com`, - 'invalid-redirect', - ), + const fetchError = new FetchError( + `uri requested responds with an invalid redirect URL: http://test.com`, + 'invalid-redirect', ); + queueError(fetchError); + queueError(fetchError); const request = async () => { await client.request(query); }; await expect(request).rejects.toThrow(HttpRequestError); + await expect(request).rejects.toThrow('invalid redirect'); }); it('allows overriding the API version', async () => { diff --git a/packages/shopify-api/lib/clients/common.ts b/packages/shopify-api/lib/clients/common.ts index 2fb1204eb..a29d54614 100644 --- a/packages/shopify-api/lib/clients/common.ts +++ b/packages/shopify-api/lib/clients/common.ts @@ -62,11 +62,9 @@ export function throwFailedRequest( response?: Response, ): never { if (typeof response === 'undefined') { + const message = body?.errors?.message ?? ''; throw new ShopifyErrors.HttpRequestError( - 'Http request error, no response available', - { - body, - }, + `Http request error, no response available: ${message}`, ); } diff --git a/packages/shopify-api/lib/clients/storefront/__tests__/storefront_client.test.ts b/packages/shopify-api/lib/clients/storefront/__tests__/storefront_client.test.ts index 835345bed..7422a202b 100644 --- a/packages/shopify-api/lib/clients/storefront/__tests__/storefront_client.test.ts +++ b/packages/shopify-api/lib/clients/storefront/__tests__/storefront_client.test.ts @@ -181,17 +181,18 @@ describe('Storefront GraphQL client', () => { apiVersion: '2020-01' as any as ApiVersion, }); - queueError( - new FetchError( - `uri requested responds with an invalid redirect URL: http://test.com`, - 'invalid-redirect', - ), + const fetchError = new FetchError( + `uri requested responds with an invalid redirect URL: http://test.com`, + 'invalid-redirect', ); + queueError(fetchError); + queueError(fetchError); const request = async () => { await client.request(QUERY); }; await expect(request).rejects.toThrow(HttpRequestError); + await expect(request).rejects.toThrow('invalid redirect'); }); });