From 2d622a144cc3d3d8d30129af863cde013a0d6102 Mon Sep 17 00:00:00 2001 From: Ajima Chukwuemeka <32770340+ajimae@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:37:46 +0100 Subject: [PATCH] fix(sdk-client-v3): fixing support ticket ST-29803 (#864) - fix issues with SDK return 200 and undefined response body on error --- .changeset/ninety-pugs-decide.md | 5 +++ .../src/middleware/create-http-middleware.ts | 3 +- packages/sdk-client-v3/src/utils/executor.ts | 2 +- .../tests/http.test/http-middleware.test.ts | 45 ++++++++++++++++++- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 .changeset/ninety-pugs-decide.md diff --git a/.changeset/ninety-pugs-decide.md b/.changeset/ninety-pugs-decide.md new file mode 100644 index 000000000..eb4daee64 --- /dev/null +++ b/.changeset/ninety-pugs-decide.md @@ -0,0 +1,5 @@ +--- +'@commercetools/ts-client': patch +--- + +Fix issue with sdk-client-v3 returning undefined body on error diff --git a/packages/sdk-client-v3/src/middleware/create-http-middleware.ts b/packages/sdk-client-v3/src/middleware/create-http-middleware.ts index 9ac6d3b45..e35d08824 100644 --- a/packages/sdk-client-v3/src/middleware/create-http-middleware.ts +++ b/packages/sdk-client-v3/src/middleware/create-http-middleware.ts @@ -130,7 +130,8 @@ async function executeRequest({ }) throw { - body: error, + // because body and error should be mutually exclusive + body: null, error, } } finally { diff --git a/packages/sdk-client-v3/src/utils/executor.ts b/packages/sdk-client-v3/src/utils/executor.ts index f21a9e057..28f9a5fab 100644 --- a/packages/sdk-client-v3/src/utils/executor.ts +++ b/packages/sdk-client-v3/src/utils/executor.ts @@ -141,7 +141,7 @@ export default async function executor(request: HttpClientConfig) { data = response.data || response } } catch (err) { - data = result + throw err } return { diff --git a/packages/sdk-client-v3/tests/http.test/http-middleware.test.ts b/packages/sdk-client-v3/tests/http.test/http-middleware.test.ts index cbff13527..b1a61306e 100644 --- a/packages/sdk-client-v3/tests/http.test/http-middleware.test.ts +++ b/packages/sdk-client-v3/tests/http.test/http-middleware.test.ts @@ -394,14 +394,14 @@ describe('Http Middleware.', () => { } }) - test('should return a parse a text encoded response as json', async () => { + test('should return a text encoded response as a parsed json', async () => { const _response = { data: {}, statusCode: 200, } const response = createTestResponse({ - text: () => _response, + text: () => JSON.stringify(_response), }) const request = createTestRequest({ @@ -429,6 +429,47 @@ describe('Http Middleware.', () => { await createHttpMiddleware(httpMiddlewareOptions)(next)(request) }) + test('should throw an error if it is unable to parse a text response', async () => { + const _response = { + data: {}, + statusCode: 200, + } + + const response = createTestResponse({ + text: () => _response, // this is not a text response + }) + + const request = createTestRequest({ + uri: '/error-url/retry', + method: 'POST', + body: { id: 'test-id' }, + headers: { + 'Content-Type': 'image/jpeg', + }, + }) + + const httpMiddlewareOptions: HttpMiddlewareOptions = { + host: 'http://api-host.com', + httpClient: jest.fn(() => response), + enableRetry: false, + } + + const next = (req: MiddlewareRequest) => { + expect(typeof req.response).toEqual('object') + expect(req.response?.body).toEqual(_response) + + return response + } + + try { + await createHttpMiddleware(httpMiddlewareOptions)(next)(request) + } catch (e) { + expect(e.body).toEqual(null) + expect(e.error.statusCode).toEqual(0) + expect(e.error.name).toEqual('NetworkError') + } + }) + describe('::retry test', () => { test('should throw if `retryCode` is not an array', async () => { const response = createTestResponse({