From d7c2226a661b26508ecf957ab1cb0007483c0d81 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 11 May 2021 17:50:48 +0200 Subject: [PATCH] chore: add `ECONNRESET` to `error.code` --- src/index.test.js | 35 +++++++++++++++++++---------------- src/methods/retry.js | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/index.test.js b/src/index.test.js index c342180..64a0166 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -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) => { diff --git a/src/methods/retry.js b/src/methods/retry.js index 18dec3f..e72c06c 100644 --- a/src/methods/retry.js +++ b/src/methods/retry.js @@ -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 }