refactor(client): remove unused axios dependents (#43579)
This commit is contained in:
committed by
GitHub
parent
00a81067a3
commit
48f2c02c5d
@ -155,7 +155,6 @@
|
||||
"@types/store": "2.0.2",
|
||||
"@types/validator": "13.6.3",
|
||||
"autoprefixer": "10.3.6",
|
||||
"axios": "0.21.2",
|
||||
"babel-plugin-transform-imports": "2.0.0",
|
||||
"chokidar": "3.5.2",
|
||||
"copy-webpack-plugin": "9.0.1",
|
||||
|
@ -1,17 +1,13 @@
|
||||
import type { AxiosError } from 'axios';
|
||||
import { isObject } from 'lodash-es';
|
||||
|
||||
import {
|
||||
isHandledError,
|
||||
wrapHandledError,
|
||||
unwrapHandledError,
|
||||
handledErrorSymbol,
|
||||
handleAPIError
|
||||
handledErrorSymbol
|
||||
} from './handled-error';
|
||||
import type { HandledError } from './handled-error';
|
||||
|
||||
import reportedErrorMessage from './reported-error-message';
|
||||
|
||||
describe('client/src utilities', () => {
|
||||
describe('handled-error.js', () => {
|
||||
const mockHandledErrorData = {
|
||||
@ -72,80 +68,5 @@ describe('client/src utilities', () => {
|
||||
expect(unwrapped).toEqual(mockHandledErrorData);
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleAPIError', () => {
|
||||
let reportMock: () => void;
|
||||
beforeEach(() => {
|
||||
reportMock = jest.fn();
|
||||
});
|
||||
|
||||
it('returns handled error data', () => {
|
||||
expect.assertions(3);
|
||||
const axiosErrorMock = {
|
||||
response: {
|
||||
status: 400
|
||||
}
|
||||
} as AxiosError;
|
||||
const result = handleAPIError(
|
||||
axiosErrorMock,
|
||||
{ redirectTo: '/' },
|
||||
reportMock
|
||||
);
|
||||
expect(result).toHaveProperty('type');
|
||||
expect(result).toHaveProperty('message');
|
||||
expect(result).toHaveProperty('redirectTo');
|
||||
});
|
||||
|
||||
it('does not report 4** errors', () => {
|
||||
expect.assertions(1);
|
||||
for (let i = 400; i < 500; i++) {
|
||||
const axiosErrorMock = {
|
||||
response: {
|
||||
status: i
|
||||
}
|
||||
} as AxiosError;
|
||||
handleAPIError(axiosErrorMock, { redirectTo: '/' }, reportMock);
|
||||
}
|
||||
expect(reportMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('reports on 5** errors', () => {
|
||||
const axiosErrorMock = {
|
||||
response: {
|
||||
status: 502
|
||||
}
|
||||
} as AxiosError;
|
||||
handleAPIError(axiosErrorMock, { redirectTo: '/' }, reportMock);
|
||||
expect(reportMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('returns a `reportedErrorMessage` for a 5** error', () => {
|
||||
const axiosErrorMock = {
|
||||
response: {
|
||||
status: 502
|
||||
}
|
||||
} as AxiosError;
|
||||
const result = handleAPIError(
|
||||
axiosErrorMock,
|
||||
{ redirectTo: '/' },
|
||||
reportMock
|
||||
);
|
||||
expect(result).toEqual({ ...reportedErrorMessage, redirectTo: '/' });
|
||||
});
|
||||
|
||||
it('respects a `null` redirectTo', () => {
|
||||
const axiosErrorMock = {
|
||||
response: {
|
||||
status: 400
|
||||
}
|
||||
} as AxiosError;
|
||||
const result = handleAPIError(
|
||||
axiosErrorMock,
|
||||
{ redirectTo: null },
|
||||
reportMock
|
||||
);
|
||||
expect(result.redirectTo).toBe(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,10 +1,5 @@
|
||||
import type { AxiosError, AxiosResponse } from 'axios';
|
||||
import { has } from 'lodash-es';
|
||||
|
||||
import { reportClientSideError } from './report-error';
|
||||
import reportedErrorMessage from './reported-error-message';
|
||||
import standardErrorMessage from './standard-error-message';
|
||||
|
||||
interface ErrorData {
|
||||
type?: string;
|
||||
message?: string;
|
||||
@ -38,71 +33,3 @@ export function wrapHandledError(
|
||||
};
|
||||
return err as HandledError;
|
||||
}
|
||||
|
||||
export function handle400Error(
|
||||
e: AxiosError,
|
||||
options: ErrorData = { redirectTo: '/' }
|
||||
): ErrorData {
|
||||
const { status } = e.response as AxiosResponse;
|
||||
const { redirectTo } = options;
|
||||
const flash = { ...standardErrorMessage, redirectTo };
|
||||
|
||||
switch (status) {
|
||||
case 401:
|
||||
case 403: {
|
||||
return {
|
||||
...flash,
|
||||
type: 'warn',
|
||||
message: 'flash.not-authorized'
|
||||
};
|
||||
}
|
||||
case 404: {
|
||||
return {
|
||||
...flash,
|
||||
type: 'info',
|
||||
message: 'flash.could-not-find'
|
||||
};
|
||||
}
|
||||
default: {
|
||||
return flash;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function handle500Error(
|
||||
e: AxiosError,
|
||||
options: ErrorData = {
|
||||
redirectTo: '/'
|
||||
},
|
||||
_reportClientSideError: (
|
||||
e: Error,
|
||||
message: string
|
||||
) => void = reportClientSideError
|
||||
): ErrorData {
|
||||
const { redirectTo } = options;
|
||||
_reportClientSideError(e, 'We just handled a 5** error on the client');
|
||||
return { ...reportedErrorMessage, redirectTo };
|
||||
}
|
||||
|
||||
export function handleAPIError(
|
||||
e: AxiosError,
|
||||
options: ErrorData,
|
||||
_reportClientSideError: (
|
||||
e: Error,
|
||||
message: string
|
||||
) => void = reportClientSideError
|
||||
): ErrorData {
|
||||
const { response: { status = 0 } = {} } = e;
|
||||
if (status >= 400 && status < 500) {
|
||||
return handle400Error(e, options);
|
||||
}
|
||||
if (status >= 500) {
|
||||
return handle500Error(e, options, _reportClientSideError);
|
||||
}
|
||||
const { redirectTo } = options;
|
||||
_reportClientSideError(
|
||||
e,
|
||||
'We just handled an api error on the client without an error status code'
|
||||
);
|
||||
return { ...reportedErrorMessage, redirectTo };
|
||||
}
|
||||
|
Reference in New Issue
Block a user