Skip to content

Commit

Permalink
Retry more network errors (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
willmarks committed Apr 26, 2023
1 parent 2f56710 commit f6a0abd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"commander": "^9.3.0",
"fs-extra": "^9.1.0",
"graphql": "^16.3.0",
"is-retry-allowed": "^2.2.0",
"json-to-graphql-query": "^2.2.4",
"lodash": "^4.17.21",
"luxon": "^3.1.1",
Expand Down
15 changes: 12 additions & 3 deletions src/axios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios, {AxiosInstance, AxiosRequestConfig} from 'axios';
import axios, {AxiosError, AxiosInstance, AxiosRequestConfig} from 'axios';
import axiosRetry, {IAxiosRetryConfig, isRetryableError} from 'axios-retry';
import isRetryAllowed from 'is-retry-allowed';
import {Logger} from 'pino';

import {wrapApiError} from './errors';
Expand Down Expand Up @@ -31,11 +32,19 @@ export function makeAxiosInstanceWithRetry(
retries = DEFAULT_RETRIES,
delay = DEFAULT_RETRY_DELAY
): AxiosInstance {
const isNetworkError = (error: AxiosError<any>): boolean => {
return (
!error.response &&
Boolean(error.code) && // Prevents retrying cancelled requests
isRetryAllowed(error) // Prevents retrying unsafe errors
);
};

return makeAxiosInstance(config, {
retries,
retryCondition: (error) => {
// Timeouts should be retryable
return error.code === 'ECONNABORTED' || isRetryableError(error);
return isNetworkError(error) || isRetryableError(error);
},
retryDelay: (retryNumber, error) => {
if (logger) {
Expand All @@ -45,6 +54,6 @@ export function makeAxiosInstanceWithRetry(
}
return retryNumber * delay;
},
shouldResetTimeout: true
shouldResetTimeout: true,
});
}

0 comments on commit f6a0abd

Please sign in to comment.