Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 7948959

Browse files
authored
Merge pull request #418 from netlify/fix/server-error-retry
fix: retry on `5**` responses
2 parents d573ae8 + eb49f84 commit 7948959

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/index.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,22 @@ test('Can timeout access token polling', async (t) => {
470470
t.false(scope.isDone())
471471
})
472472

473+
test('Retries on server errors', async (t) => {
474+
const accountId = uuidv4()
475+
const expectedResponse = { test: 'test' }
476+
const scope = nock(origin)
477+
.get(`${pathPrefix}/accounts/${accountId}`)
478+
.reply(500)
479+
.get(`${pathPrefix}/accounts/${accountId}`)
480+
.reply(200, expectedResponse)
481+
482+
const client = getClient()
483+
const response = await client.getAccount({ account_id: accountId })
484+
485+
t.deepEqual(response, expectedResponse)
486+
t.true(scope.isDone())
487+
})
488+
473489
test('Handles API rate limiting', async (t) => {
474490
const accountId = uuidv4()
475491
const retryAtMs = Date.now() + TEST_RATE_LIMIT_DELAY

src/methods/retry.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// - when receiving a rate limiting response
33
// - on network failures due to timeouts
44
const shouldRetry = function ({ response = {}, error = {} }) {
5-
return response.status === RATE_LIMIT_STATUS || RETRY_ERROR_CODES.has(error.code)
5+
return isRetryStatus(response) || RETRY_ERROR_CODES.has(error.code)
6+
}
7+
8+
const isRetryStatus = function ({ status }) {
9+
return typeof status === 'number' && (status === RATE_LIMIT_STATUS || String(status).startsWith('5'))
610
}
711

812
const waitForRetry = async function (response) {

0 commit comments

Comments
 (0)