From ce088031d7eb54188cd8bb03f2336f0bac6e6114 Mon Sep 17 00:00:00 2001 From: Brett Willemsen Date: Tue, 23 Mar 2021 16:22:37 -0400 Subject: [PATCH 1/2] Fix missing catch statements on network errors. --- src/api.test.ts | 10 ++++++++++ src/common.ts | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/api.test.ts b/src/api.test.ts index 50bff06..3f917f0 100644 --- a/src/api.test.ts +++ b/src/api.test.ts @@ -304,3 +304,13 @@ test('API `delete` calls with shorthand `get` should succeed', async done => { expect(response.data).toEqual(EMPTY_BODY); done(); }); + + +test('API catches ETIMEDOUT error', async done => { + nock('https://api.pagerduty.com').get('/incidents').replyWithError({code: 'ETIMEDOUT'}) + + const pd = api({token: 'someToken1234567890'}); + + await expect(pd.get('/incidents')).rejects.toThrow('request to https://api.pagerduty.com/incidents failed, reason: undefined'); + done(); +}); diff --git a/src/common.ts b/src/common.ts index 51be4b4..0fc3249 100644 --- a/src/common.ts +++ b/src/common.ts @@ -59,7 +59,7 @@ function fetch_retry( } else { resolve(response); } - }); + }).catch(reject); }); } From d61072f225545e5154420a5af44f15803048ba77 Mon Sep 17 00:00:00 2001 From: Brett Willemsen Date: Tue, 23 Mar 2021 16:26:28 -0400 Subject: [PATCH 2/2] Fix linting. --- src/api.test.ts | 9 ++++++--- src/common.ts | 32 +++++++++++++++++--------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/api.test.ts b/src/api.test.ts index 3f917f0..4adec83 100644 --- a/src/api.test.ts +++ b/src/api.test.ts @@ -305,12 +305,15 @@ test('API `delete` calls with shorthand `get` should succeed', async done => { done(); }); - test('API catches ETIMEDOUT error', async done => { - nock('https://api.pagerduty.com').get('/incidents').replyWithError({code: 'ETIMEDOUT'}) + nock('https://api.pagerduty.com') + .get('/incidents') + .replyWithError({code: 'ETIMEDOUT'}); const pd = api({token: 'someToken1234567890'}); - await expect(pd.get('/incidents')).rejects.toThrow('request to https://api.pagerduty.com/incidents failed, reason: undefined'); + await expect(pd.get('/incidents')).rejects.toThrow( + 'request to https://api.pagerduty.com/incidents failed, reason: undefined' + ); done(); }); diff --git a/src/common.ts b/src/common.ts index 0fc3249..d4a0664 100644 --- a/src/common.ts +++ b/src/common.ts @@ -45,21 +45,23 @@ function fetch_retry( options: RequestOptions ): Promise { return new Promise((resolve, reject) => { - fetch(url, options).then(response => { - // We don't want to `reject` when retries have finished - // Instead simply stop trying and return. - if (retries === 0) return resolve(response); - if (response.status === 429) { - const {retryTimeout = 20000} = options; - retryTimeoutPromise(retryTimeout).then(() => { - fetch_retry(url, retries - 1, options) - .then(resolve) - .catch(reject); - }); - } else { - resolve(response); - } - }).catch(reject); + fetch(url, options) + .then(response => { + // We don't want to `reject` when retries have finished + // Instead simply stop trying and return. + if (retries === 0) return resolve(response); + if (response.status === 429) { + const {retryTimeout = 20000} = options; + retryTimeoutPromise(retryTimeout).then(() => { + fetch_retry(url, retries - 1, options) + .then(resolve) + .catch(reject); + }); + } else { + resolve(response); + } + }) + .catch(reject); }); }