Skip to content
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
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 @@ -9,10 +9,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
50 changes: 49 additions & 1 deletion test/retryPolicy/delivery-sdk-handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,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 @@ -203,7 +249,9 @@ describe('retryResponseErrorHandler', () => {

it('should resolve the promise to 408 error if retryOnError is true and error code is ECONNABORTED', 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 Down
Loading