Skip to content

Commit

Permalink
fix: use 'errors.getMessage' to determine error message in AxiosWrapper
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 committed Oct 23, 2023
1 parent 5f0a611 commit 6ed8cc0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* Red Hat, Inc. - initial API and implementation
*/

import mockAxios, { AxiosInstance } from 'axios';
import common from '@eclipse-che/common';
import mockAxios, { AxiosInstance, AxiosResponse } from 'axios';

import {
AxiosWrapper,
Expand Down Expand Up @@ -46,7 +47,7 @@ describe('axiosWrapper', () => {
expect(axiosGetSpy).toBeCalledTimes(1);
});

it('should retry 0 time without specifi error message', async () => {
it('should retry 0 time without specific error message', async () => {
const expectedData = { data: 'some-data' };
axiosGetMock.mockReturnValue(new Promise(resolve => resolve(expectedData)));

Expand All @@ -71,7 +72,22 @@ describe('axiosWrapper', () => {
expect(axiosGetSpy).toBeCalledTimes(2);
});

it('should retry 1 time without specif error message', async () => {
it('should retry 1 time with Bearer Token Authorization is required axios response error message', async () => {
const expectedData = { data: 'some-data' };
axiosGetMock
.mockRejectedValueOnce(createAxiosResponseError(bearerTokenAuthorizationIsRequiredErrorMsg))
.mockReturnValue(new Promise(resolve => resolve(expectedData)));

const result = await new AxiosWrapper(
axiosInstance,
bearerTokenAuthorizationIsRequiredErrorMsg,
).get('some-url');

expect(result).toEqual(expectedData);
expect(axiosGetSpy).toBeCalledTimes(2);
});

it('should retry 1 time without specifc error message', async () => {
const expectedData = { data: 'some-data' };
axiosGetMock
.mockRejectedValueOnce(new Error('some error message'))
Expand Down Expand Up @@ -99,6 +115,22 @@ describe('axiosWrapper', () => {
expect(axiosGetSpy).toBeCalledTimes(3);
});

it('should retry 2 times with Bearer Token Authorization is required axios response error message', async () => {
const expectedData = { data: 'some-data' };
axiosGetMock
.mockRejectedValueOnce(createAxiosResponseError(bearerTokenAuthorizationIsRequiredErrorMsg))
.mockRejectedValueOnce(createAxiosResponseError(bearerTokenAuthorizationIsRequiredErrorMsg))
.mockReturnValue(new Promise(resolve => resolve(expectedData)));

const result = await new AxiosWrapper(
axiosInstance,
bearerTokenAuthorizationIsRequiredErrorMsg,
).get('some-url');

expect(result).toEqual(expectedData);
expect(axiosGetSpy).toBeCalledTimes(3);
});

it('should retry 2 times without specific error message', async () => {
const expectedData = { data: 'some-data' };
axiosGetMock
Expand Down Expand Up @@ -130,6 +162,25 @@ describe('axiosWrapper', () => {
}
});

it('should fail after 3 times with Bearer Token Authorization is required axios response error message', async () => {
axiosGetMock
.mockRejectedValueOnce(createAxiosResponseError(bearerTokenAuthorizationIsRequiredErrorMsg))
.mockRejectedValueOnce(createAxiosResponseError(bearerTokenAuthorizationIsRequiredErrorMsg))
.mockRejectedValueOnce(createAxiosResponseError(bearerTokenAuthorizationIsRequiredErrorMsg))
.mockRejectedValue(createAxiosResponseError(bearerTokenAuthorizationIsRequiredErrorMsg));

try {
await new AxiosWrapper(axiosInstance, bearerTokenAuthorizationIsRequiredErrorMsg).get(
'some-url',
);
fail('should fail');
} catch (e: any) {
expect(common.helpers.errors.includesAxiosResponse(e)).toBeTruthy();
expect(e.response.data).toEqual(bearerTokenAuthorizationIsRequiredErrorMsg);
expect(axiosGetSpy).toBeCalledTimes(4);
}
});

it('should fail after 3 times without specific error message', async () => {
axiosGetMock
.mockRejectedValueOnce(new Error('error 1'))
Expand All @@ -146,3 +197,15 @@ describe('axiosWrapper', () => {
}
});
});

function createAxiosResponseError(message: string): { response: AxiosResponse } {
return {
response: {
data: message,
status: 401,
statusText: 'Unauthorized',
headers: {},
config: {},
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Red Hat, Inc. - initial API and implementation
*/

import common from '@eclipse-che/common';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';

import { delay } from '@/services/helpers/delay';
Expand Down Expand Up @@ -97,7 +98,8 @@ export class AxiosWrapper {
} catch (err) {
if (
!retry ||
(this.errorMessagesToRetry && !(err as Error)?.message?.includes(this.errorMessagesToRetry))
(this.errorMessagesToRetry &&
!common.helpers.errors.getMessage(err).includes(this.errorMessagesToRetry))
) {
throw err;
}
Expand Down

0 comments on commit 6ed8cc0

Please sign in to comment.