diff --git a/src/index.ts b/src/index.ts index cba1f3b..6742ff4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -42,10 +42,15 @@ export interface RetryConfig { */ statusCodesToRetry?: number[][]; + /** + * Function to invoke when error occurred. + */ + onError?: (error: AxiosError) => void | Promise; + /** * Function to invoke when a retry attempt is made. */ - onRetryAttempt?: (error: AxiosError) => void; + onRetryAttempt?: (error: AxiosError) => void | Promise; /** * Function to invoke which determines if you should retry @@ -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!)); } diff --git a/test/index.ts b/test/index.ts index 8467fe5..63304b0 100644 --- a/test/index.ts +++ b/test/index.ts @@ -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', }, @@ -660,7 +660,7 @@ describe('retry-axios', () => { const axiosPromise = axios({ url, raxConfig: { - onRetryAttempt: resolve, + onError: resolve, backoffType: 'static', retryDelay: 10_000, }, @@ -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',