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

Commit d0328e9

Browse files
feat!: stop retrying on 5xx responses (#726)
1 parent 1e76962 commit d0328e9

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/index.test.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,17 +494,38 @@ test('Can timeout access token polling', async (t) => {
494494
t.false(scope.isDone())
495495
})
496496

497-
test('Retries on server errors', async (t) => {
497+
test('Does not retry on server errors', async (t) => {
498+
const errorMessage = 'Something went zap!'
498499
const accountId = uuidv4()
499500
const expectedResponse = { test: 'test' }
500501
const scope = nock(origin)
501502
.get(`${pathPrefix}/accounts/${accountId}`)
502-
.reply(500)
503+
.reply(500, errorMessage)
503504
.get(`${pathPrefix}/accounts/${accountId}`)
504505
.reply(200, expectedResponse)
505506

506507
const client = getClient()
507-
const response = await client.getAccount({ account_id: accountId })
508+
const error = await t.throwsAsync(client.getAccount({ account_id: accountId }))
509+
510+
t.is(error.status, 500)
511+
t.is(error.message, errorMessage)
512+
t.false(scope.isDone())
513+
})
514+
515+
test('Retries on server errors for the `getLatestPluginRuns` endpoint', async (t) => {
516+
const packages = 'foo'
517+
const siteId = uuidv4()
518+
const expectedResponse = { test: 'test' }
519+
const scope = nock(origin)
520+
.get(`${pathPrefix}/sites/${siteId}/plugin_runs/latest`)
521+
.query({ packages })
522+
.reply(500)
523+
.get(`${pathPrefix}/sites/${siteId}/plugin_runs/latest`)
524+
.query({ packages })
525+
.reply(200, expectedResponse)
526+
527+
const client = getClient()
528+
const response = await client.getLatestPluginRuns({ site_id: siteId, packages })
508529

509530
t.deepEqual(response, expectedResponse)
510531
t.true(scope.isDone())

src/methods/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const makeRequestOrRetry = async function ({ url, method, defaultHeaders, agent,
7676
const optsA = getOpts({ method, defaultHeaders, agent, requestParams, opts })
7777
const { response, error } = await makeRequest(url, optsA)
7878

79-
if (shouldRetry({ response, error }) && index !== MAX_RETRY) {
79+
if (shouldRetry({ response, error, method }) && index !== MAX_RETRY) {
8080
await waitForRetry(response)
8181
continue
8282
}

src/methods/retry.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// We retry:
22
// - when receiving a rate limiting response
33
// - on network failures due to timeouts
4-
export const shouldRetry = function ({ response = {}, error = {} }) {
5-
return isRetryStatus(response) || RETRY_ERROR_CODES.has(error.code)
6-
}
4+
export const shouldRetry = function ({ response = {}, error = {}, method = {} }) {
5+
if (response.status === RATE_LIMIT_STATUS || RETRY_ERROR_CODES.has(error.code)) {
6+
return true
7+
}
8+
9+
// Special case for the `getLatestPluginRuns` endpoint.
10+
// See https://github.com/netlify/bitballoon/issues/9616.
11+
if (method.operationId === 'getLatestPluginRuns' && response.status === 500) {
12+
return true
13+
}
714

8-
const isRetryStatus = function ({ status }) {
9-
return typeof status === 'number' && (status === RATE_LIMIT_STATUS || String(status).startsWith('5'))
15+
return false
1016
}
1117

1218
export const waitForRetry = async function (response) {

0 commit comments

Comments
 (0)