Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/lib/retryPolicy/delivery-sdk-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ declare module 'axios' {
}
}

const TRANSIENT_NETWORK_ERROR_CODES = ['ECONNABORTED', 'ETIMEDOUT', 'ECONNRESET', 'EPIPE', 'EAI_AGAIN'];

const defaultConfig = {
maxRequests: 5,
retryLimit: 5,
retryDelay: 300,
retryCondition: (error: any) => !error.response && TRANSIENT_NETWORK_ERROR_CODES.includes(error.code),
};

const DEFAULT_RETRY_DELAY_MS = 300;
Expand Down
54 changes: 52 additions & 2 deletions test/retryPolicy/delivery-sdk-handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,52 @@ describe('retryResponseErrorHandler', () => {
jest.useRealTimers();
});

it.each(['ECONNABORTED', 'ETIMEDOUT', 'ECONNRESET', 'EPIPE', 'EAI_AGAIN'])(
'should retry transient network error %s by default when no custom retryCondition is configured',
async (code) => {
const error = {
config: { retryOnError: true, retryCount: 1, method: 'get', url: '/default-retry' },
code,
message: `simulated ${code}`,
};
// No retryCondition here - mirrors the raw StackConfig a real stack() caller passes.
const config = { retryLimit: 3, retryDelay: 50 };
const client = axios.create();

mock.onGet('/default-retry').reply(200, { success: true });

jest.useFakeTimers();

const responsePromise = retryResponseErrorHandler(error, config, client);
jest.advanceTimersByTime(50);

const response = (await responsePromise) as AxiosResponse;
expect(response.status).toBe(200);

jest.useRealTimers();
}
);

it.each(['ENOTFOUND', 'ECONNREFUSED'])(
'should not retry non-transient network error %s by default when no custom retryCondition is configured',
async (code) => {
const error = {
config: { retryOnError: true, retryCount: 1 },
code,
message: `simulated ${code}`,
};
const config = { retryLimit: 3 };
const client = axios.create();

try {
await retryResponseErrorHandler(error, config, client);
fail(`Expected retryResponseErrorHandler to throw for ${code}`);
} catch (err) {
expect(err).toEqual(error);
}
}
);

it('should rethrow network errors when retryCondition returns false', async () => {
const error = {
config: { retryOnError: true, retryCount: 1 },
Expand Down Expand Up @@ -204,7 +250,9 @@ describe('retryResponseErrorHandler', () => {

it('should throw a real Error with the timeout duration and a TIMEOUT code when ECONNABORTED occurs', async () => {
const error = { config: { retryOnError: true, retryCount: 1 }, code: 'ECONNABORTED' };
const config = { retryLimit: 5, timeout: 1000 };
// retryCondition explicitly disabled here to isolate the non-retried ECONNABORTED throw path,
// since ECONNABORTED is retried by default now (see "should retry transient network error" tests).
const config = { retryLimit: 5, timeout: 1000, retryCondition: () => false };
const client = axios.create();
try {
await retryResponseErrorHandler(error, config, client);
Expand All @@ -219,7 +267,9 @@ describe('retryResponseErrorHandler', () => {
});
it('should classify a request timeout distinctly instead of as an unknown error', async () => {
const error = { config: { retryOnError: true, retryCount: 1 }, code: 'ECONNABORTED' };
const config = { retryLimit: 5, timeout: 1000 };
// retryCondition explicitly disabled to isolate the non-retried classification path -
// ECONNABORTED is retried by default now (see "should retry transient network error" tests).
const config = { retryLimit: 5, timeout: 1000, retryCondition: () => false };
const client = axios.create();

let thrown: any;
Expand Down
Loading