diff --git a/src/api.test.ts b/src/api.test.ts index 50bff06..4adec83 100644 --- a/src/api.test.ts +++ b/src/api.test.ts @@ -304,3 +304,16 @@ 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..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); - } - }); + 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); }); }