Skip to content

Commit

Permalink
feat(http-error-retryCodes): improvement
Browse files Browse the repository at this point in the history
- improve and refactor code
- add check to ensure retryCodes is not null
  • Loading branch information
ajimae committed Feb 14, 2022
1 parent 1cadbff commit b7c4a59
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/sdk-middleware-http/src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export default function createHttpMiddleware({
fetchFunction = fetch
}

if (!Array.isArray(retryCodes)) {
throw new Error(
'`retryCodes` option must be an array of retry status (error) codes.'
)
}

return (next: Next): Next =>
(request: MiddlewareRequest, response: MiddlewareResponse) => {
let abortController: any
Expand Down Expand Up @@ -232,7 +238,7 @@ export default function createHttpMiddleware({

if (
enableRetry &&
(res.status === 503 || retryCodes.includes(res.status))
(res.status === 503 || (retryCodes?.indexOf(res.status) !== -1))
) {
if (retryCount < maxRetries) {
setTimeout(
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk-middleware-http/test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ describe('Http', () => {
)
})

test('throw when a non-array option is passed as retryCodes in the httpMiddlewareOptions', () => {
expect(() => {
createHttpMiddleware({ host: testHost, retryCodes: null, fetch })
}).toThrow(
new Error(
'`retryCodes` option must be an array of retry status (error) codes.'
)
)
})

test('execute a get request (success)', () =>
new Promise((resolve, reject) => {
const request = createTestRequest({
Expand Down

0 comments on commit b7c4a59

Please sign in to comment.