Skip to content

Commit

Permalink
Fix Unhandled Promise Rejection Error (#1880)
Browse files Browse the repository at this point in the history
* :

* chore(test-coverage): fix test coverage

- fix test coverage by adding more tests
- add a return statement to analyzeExcelStream function
  • Loading branch information
ajimae authored Aug 17, 2023
1 parent 2175f68 commit 9a53db0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/product-json-to-xlsx/test/writer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
33 changes: 29 additions & 4 deletions packages/sdk-middleware-http/src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down
72 changes: 72 additions & 0 deletions packages/sdk-middleware-http/test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit 9a53db0

Please sign in to comment.