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

fix: Call onRetryAttempt *after* backoff timeout #244

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ export interface RetryConfig {
*/
statusCodesToRetry?: number[][];

/**
* Function to invoke when error occurred.
*/
onError?: (error: AxiosError) => void | Promise<void>;

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

/**
* Function to invoke which determines if you should retry
Expand Down Expand Up @@ -285,17 +290,14 @@ async function onError(instance: AxiosInstance, error: AxiosError) {
setTimeout(resolve, delay);
});

// Notify the user if they added an `onRetryAttempt` handler
if (config.onRetryAttempt) {
config.onRetryAttempt(axiosError);
if (config.onError) {
await config.onError(axiosError);
}

const onRetryAttemptPromise = Promise.resolve();

// Return the promise in which recalls axios to retry the request
return Promise.resolve()
.then(async () => onBackoffPromise)
.then(async () => onRetryAttemptPromise)
.then(async () => config.onRetryAttempt?.(axiosError))
.then(async () => config.instance!.request(axiosError.config!));
}

Expand Down
6 changes: 3 additions & 3 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ describe('retry-axios', () => {
const axiosPromise = axios({
url,
raxConfig: {
onRetryAttempt: resolve,
onError: resolve,
retryDelay: 10_000, // Higher default to ensure Retry-After is used
backoffType: 'static',
},
Expand Down Expand Up @@ -660,7 +660,7 @@ describe('retry-axios', () => {
const axiosPromise = axios({
url,
raxConfig: {
onRetryAttempt: resolve,
onError: resolve,
backoffType: 'static',
retryDelay: 10_000,
},
Expand Down Expand Up @@ -710,7 +710,7 @@ describe('retry-axios', () => {
const axiosPromise = axios({
url,
raxConfig: {
onRetryAttempt: resolve,
onError: resolve,
retryDelay: 10_000, // Higher default to ensure maxRetryDelay is used
maxRetryDelay: 5000,
backoffType: 'exponential',
Expand Down
Loading