Skip to content

Commit

Permalink
Merge pull request #130 from PagerDuty/fixNetworkCatch
Browse files Browse the repository at this point in the history
Fix missing catch statements on network errors.
  • Loading branch information
Brett Willemsen authored Mar 23, 2021
2 parents 20f466b + d61072f commit 44375e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
32 changes: 17 additions & 15 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,23 @@ function fetch_retry(
options: RequestOptions
): Promise<Response> {
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);
});
}

Expand Down

0 comments on commit 44375e8

Please sign in to comment.