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
10 changes: 3 additions & 7 deletions src/lib/retryPolicy/delivery-sdk-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-throw-literal */
import axios, { InternalAxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
import { ERROR_MESSAGES } from '../error-messages';

Expand Down Expand Up @@ -59,12 +58,9 @@ export const retryResponseErrorHandler = (error: any, config: any, axiosInstance
}

if (error.code === 'ECONNABORTED') {
const customError = {
error_message: ERROR_MESSAGES.RETRY.TIMEOUT_EXCEEDED(config.timeout),
error_code: ERROR_MESSAGES.ERROR_CODES.TIMEOUT,
errors: null,
};
throw customError; // Throw customError object
const timeoutError = new Error(ERROR_MESSAGES.RETRY.TIMEOUT_EXCEEDED(config.timeout));
(timeoutError as any).code = ERROR_MESSAGES.ERROR_CODES.TIMEOUT;
throw timeoutError;
}

throw error;
Expand Down
33 changes: 25 additions & 8 deletions test/retryPolicy/delivery-sdk-handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getRetryDelay,
} from '../../src/lib/retryPolicy/delivery-sdk-handlers';
import MockAdapter from 'axios-mock-adapter';
import { APIError } from '../../src/lib/api-error';

describe('retryRequestHandler', () => {
it('should add retryCount to the request config', () => {
Expand Down Expand Up @@ -201,22 +202,38 @@ describe('retryResponseErrorHandler', () => {
jest.useRealTimers();
});

it('should resolve the promise to 408 error if retryOnError is true and error code is ECONNABORTED', async () => {
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 };
const client = axios.create();
try {
await retryResponseErrorHandler(error, config, client);
fail('Expected retryResponseErrorHandler to throw an error');
} catch (err) {
expect(err).toEqual(
expect.objectContaining({
error_code: 408,
error_message: `Request timeout of ${config.timeout}ms exceeded. Please try again or increase the timeout value in your configuration.`,
errors: null,
})
} catch (err: any) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe(
`Request timeout of ${config.timeout}ms exceeded. Please try again or increase the timeout value in your configuration.`
);
expect(err.code).toBe(408);
}
});
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 };
const client = axios.create();

let thrown: any;
try {
await retryResponseErrorHandler(error, config, client);
fail('Expected retryResponseErrorHandler to throw an error');
} catch (err) {
thrown = err;
}

const apiError = APIError.fromAxiosError(thrown);

expect(apiError.error_code).toBe(408);
expect(apiError.error_message).toContain('timeout');
});
it('should reject the promise if response status is 429 and retryCount exceeds retryLimit', async () => {
const error = {
Expand Down
Loading