Skip to content

Commit

Permalink
fix: don't retry a request that is aborted intentionally (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Nov 25, 2019
1 parent 2e076ce commit ba9777b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export async function getRetryConfig(err: GaxiosError) {
function shouldRetryRequest(err: GaxiosError) {
const config = getConfig(err);

// node-fetch raises an AbortError if signaled:
// https://github.com/bitinn/node-fetch#request-cancellation-with-abortsignal
if (err.name === 'AbortError') {
return false;
}

// If there's no config, or retries are disabled, return.
if (!config || config.retry === 0) {
return false;
Expand Down
20 changes: 20 additions & 0 deletions test/test.retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import assert from 'assert';
import nock from 'nock';

import {AbortController} from 'abort-controller';
import {GaxiosError, GaxiosOptions, request, Gaxios} from '../src';

const assertRejects = require('assert-rejects');
Expand Down Expand Up @@ -102,6 +103,25 @@ describe('🛸 retry & exponential backoff', () => {
scope.done();
});

it('should not retry if user aborted request', async () => {
const ac = new AbortController();
const config: GaxiosOptions = {
method: 'GET',
url: 'https://google.com',
signal: ac.signal,
retryConfig: {retry: 10, noResponseRetries: 10},
};
const req = request(config);
ac.abort();
try {
await req;
throw Error('unreachable');
} catch (err) {
assert(err.config);
assert.strictEqual(err.config.retryConfig.currentRetryAttempt, 0);
}
});

it('should retry at least the configured number of times', async () => {
const body = {dippy: '🥚'};
const scopes = [
Expand Down

0 comments on commit ba9777b

Please sign in to comment.