Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,22 +542,25 @@ test('Gives up retrying on API rate limiting after a timeout', async (t) => {
t.false(scope.isDone())
})

test('Retries on ETIMEDOUT connection errors', async (t) => {
const accountId = uuidv4()
const retryAtMs = Date.now() + TEST_RATE_LIMIT_DELAY
const expectedResponse = { test: 'test' }
const scope = nock(origin)
.get(`${pathPrefix}/accounts/${accountId}`)
.replyWithError({ code: 'ETIMEDOUT' })
.get(`${pathPrefix}/accounts/${accountId}`)
.reply(200, expectedResponse)

const client = getClient()
const response = await client.getAccount({ account_id: accountId })

t.true(Date.now() >= retryAtMs)
t.deepEqual(response, expectedResponse)
t.true(scope.isDone())
const errorCodes = ['ETIMEDOUT', 'ECONNRESET']
errorCodes.forEach((code) => {
test(`Retries on ${code} connection errors`, async (t) => {
const accountId = uuidv4()
const retryAtMs = Date.now() + TEST_RATE_LIMIT_DELAY
const expectedResponse = { test: 'test' }
const scope = nock(origin)
.get(`${pathPrefix}/accounts/${accountId}`)
.replyWithError({ code })
.get(`${pathPrefix}/accounts/${accountId}`)
.reply(200, expectedResponse)

const client = getClient()
const response = await client.getAccount({ account_id: accountId })

t.true(Date.now() >= retryAtMs)
t.deepEqual(response, expectedResponse)
t.true(scope.isDone())
})
})

test('Recreates a function body when handling API rate limiting', async (t) => {
Expand Down
2 changes: 1 addition & 1 deletion src/methods/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ const SECS_TO_MSECS = 1e3
const MAX_RETRY = 10
const RATE_LIMIT_STATUS = 429
const RATE_LIMIT_HEADER = 'X-RateLimit-Reset'
const RETRY_ERROR_CODES = new Set(['ETIMEDOUT'])
const RETRY_ERROR_CODES = new Set(['ETIMEDOUT', 'ECONNRESET'])

module.exports = { shouldRetry, waitForRetry, MAX_RETRY }