From 9a53db030987ff210a869f857cfca1390b6ee5ef Mon Sep 17 00:00:00 2001 From: Ajima Chukwuemeka <32770340+ajimae@users.noreply.github.com> Date: Thu, 17 Aug 2023 12:56:23 +0200 Subject: [PATCH] Fix Unhandled Promise Rejection Error (#1880) * : * chore(test-coverage): fix test coverage - fix test coverage by adding more tests - add a return statement to analyzeExcelStream function --- .../product-json-to-xlsx/test/writer.spec.js | 2 +- packages/sdk-middleware-http/src/http.js | 33 +++++++-- .../sdk-middleware-http/test/http.spec.js | 72 +++++++++++++++++++ 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/packages/product-json-to-xlsx/test/writer.spec.js b/packages/product-json-to-xlsx/test/writer.spec.js index 560cfc3cb..c45efe5cf 100644 --- a/packages/product-json-to-xlsx/test/writer.spec.js +++ b/packages/product-json-to-xlsx/test/writer.spec.js @@ -35,7 +35,7 @@ async function analyzeExcelStream(stream) { const workbook = new Excel.Workbook() await workbook.xlsx.read(stream) - analyzeExcelWorkbook(workbook) + return analyzeExcelWorkbook(workbook) } describe('Writer', () => { diff --git a/packages/sdk-middleware-http/src/http.js b/packages/sdk-middleware-http/src/http.js index e6b805615..6333f7a36 100644 --- a/packages/sdk-middleware-http/src/http.js +++ b/packages/sdk-middleware-http/src/http.js @@ -39,9 +39,9 @@ function calcDelayDuration( if (backoff) return retryCount !== 0 // do not increase if it's the first retry ? Math.min( - Math.round((Math.random() + 1) * retryDelay * 2 ** retryCount), - maxDelay - ) + Math.round((Math.random() + 1) * retryDelay * 2 ** retryCount), + maxDelay + ) : retryDelay return retryDelay } @@ -131,7 +131,7 @@ export default function createHttpMiddleware({ requestHeader['Content-Type'] ) > -1 && typeof request.body === 'string') || - Buffer.isBuffer(request.body) + Buffer.isBuffer(request.body) ? request.body : JSON.stringify(request.body || undefined) @@ -239,7 +239,32 @@ export default function createHttpMiddleware({ maskSensitiveHeaderData ) } + next(request, parsedResponse) + }).catch((err: Error) => { + if (enableRetry) + if (retryCount < maxRetries) { + setTimeout( + executeFetch, + calcDelayDuration( + retryCount, + retryDelay, + maxRetries, + backoff, + maxDelay + ) + ) + retryCount += 1 + return + } + + const error = new NetworkError(err.message, { + originalRequest: request, + retryCount, + }) + + maskAuthData(error.originalRequest, maskSensitiveHeaderData) + next(request, { ...response, error, statusCode: 0 }) }) return } diff --git a/packages/sdk-middleware-http/test/http.spec.js b/packages/sdk-middleware-http/test/http.spec.js index 7cc9886ac..e6cef74a1 100644 --- a/packages/sdk-middleware-http/test/http.spec.js +++ b/packages/sdk-middleware-http/test/http.spec.js @@ -1578,4 +1578,76 @@ describe('Http', () => { httpMiddleware(next)(request, response) }) }) + + test('should handle error when parsing an invalid response object', () => { + expect(true).toEqual(true) + return new Promise((resolve, reject) => { + + const response = { resolve, reject } + const request = createTestRequest({ uri: '/foo/bar' }) + + const next = (req, res) => { + expect(res).toEqual({ + ...response, + error: expect.any(Error), + statusCode: 0 + }) + resolve() + } + + // Use default options + const httpMiddleware = createHttpMiddleware({ + host: testHost, + enableRetry: false, + fetch: jest.fn(() => + Promise.resolve({ + ok: true, + text: jest.fn(() => + Promise.reject(new Error('malformed response')) + ) + })) + }) + + httpMiddleware(next)(request, response) + }) + }) + + test('should handle error when parsing an invalid response object - retry', () => { + expect(true).toEqual(true) + return new Promise((resolve, reject) => { + + const response = { resolve, reject } + const request = createTestRequest({ uri: '/foo/bar' }) + + const next = (req, res) => { + expect(res).toEqual({ + ...response, + error: expect.any(Error), + statusCode: 0 + }) + resolve() + } + + // Use default options + const httpMiddleware = createHttpMiddleware({ + host: testHost, + enableRetry: true, + credentialsMode: 'omit', + retryConfig: { + maxRetries: 2, + retryDelay: 100, + backoff: false, + }, + fetch: jest.fn(() => + Promise.resolve({ + ok: true, + text: jest.fn(() => + Promise.reject(new Error('malformed response')) + ) + })) + }) + + httpMiddleware(next)(request, response) + }) + }) })