Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use 'errors.getMessage' to determine error message in AxiosWrapper #958

Merged
merged 1 commit into from
Oct 23, 2023
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
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
Loading