Skip to content

Commit

Permalink
feat: add promise support for onRetryAttempt and shouldRetry (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
atjeff authored Jan 30, 2020
1 parent ba403b3 commit 061afa3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ gaxios.request({url: '/data'}).then(...);
statusCodesToRetry?: number[][];

// Function to invoke when a retry attempt is made.
onRetryAttempt?: (err: GaxiosError) => void;
onRetryAttempt?: (err: GaxiosError) => Promise<void> | void;

// Function to invoke which determines if you should retry
shouldRetry?: (err: GaxiosError) => boolean;
shouldRetry?: (err: GaxiosError) => Promise<boolean> | boolean;

// When there is no response, the number of retries to attempt. Defaults to 2.
noResponseRetries?: number;
Expand Down
4 changes: 2 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ export interface RetryConfig {
/**
* Function to invoke when a retry attempt is made.
*/
onRetryAttempt?: (err: GaxiosError) => void;
onRetryAttempt?: (err: GaxiosError) => Promise<void> | void;

/**
* Function to invoke which determines if you should retry
*/
shouldRetry?: (err: GaxiosError) => boolean;
shouldRetry?: (err: GaxiosError) => Promise<boolean> | boolean;

/**
* When there is no response, the number of retries to attempt. Defaults to 2.
Expand Down
2 changes: 1 addition & 1 deletion src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function getRetryConfig(err: GaxiosError) {

// Determine if we should retry the request
const shouldRetryFn = config.shouldRetry || shouldRetryRequest;
if (!shouldRetryFn(err)) {
if (!(await shouldRetryFn(err))) {
return {shouldRetry: false, config: err.config};
}

Expand Down
50 changes: 47 additions & 3 deletions test/test.retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {AbortController} from 'abort-controller';
import assert from 'assert';
import nock from 'nock';

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

const assertRejects = require('assert-rejects');

Expand Down Expand Up @@ -219,6 +218,32 @@ describe('🛸 retry & exponential backoff', () => {
scopes.forEach(s => s.done());
});

it('accepts async onRetryAttempt handler', async () => {
const body = {buttered: '🥖'};
const scopes = [
nock(url)
.get('/')
.reply(500),
nock(url)
.get('/')
.reply(200, body),
];
let flipped = false;
const config: GaxiosOptions = {
url,
retryConfig: {
onRetryAttempt: async err => {
const cfg = getConfig(err);
assert.strictEqual(cfg!.currentRetryAttempt, 1);
flipped = true;
},
},
};
await request(config);
assert.strictEqual(flipped, true);
scopes.forEach(s => s.done());
});

it('should support overriding the shouldRetry method', async () => {
const scope = nock(url)
.get('/')
Expand All @@ -238,6 +263,25 @@ describe('🛸 retry & exponential backoff', () => {
scope.done();
});

it('should support overriding the shouldRetry method with a promise', async () => {
const scope = nock(url)
.get('/')
.reply(500);
const config = {
url,
retryConfig: {
shouldRetry: async () => {
return false;
},
},
};
await assertRejects(request(config), (e: Error) => {
const cfg = getConfig(e);
return cfg!.currentRetryAttempt === 0;
});
scope.done();
});

it('should retry on ENOTFOUND', async () => {
const body = {spicy: '🌮'};
const scopes = [
Expand Down

0 comments on commit 061afa3

Please sign in to comment.