Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add promise support for onRetryAttempt and shouldRetry #223

Merged
merged 7 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export async function getRetryConfig(err: GaxiosError) {

// Determine if we should retry the request
const shouldRetryFn = config.shouldRetry || shouldRetryRequest;
if (!shouldRetryFn(err)) {
const shouldRetryFnPromise = Promise.resolve(shouldRetryFn(err));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this Promise.resolve necessary, await false would just resolve false right?

if (!(await shouldRetryFnPromise)) {
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