From 48f2c02c5d477f529eb7e9b4b018c68e3afdfecf Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Mon, 27 Sep 2021 11:02:42 +0200 Subject: [PATCH] refactor(client): remove unused axios dependents (#43579) --- client/package.json | 1 - client/src/utils/handled-error.test.ts | 81 +------------------------- client/src/utils/handled-error.ts | 73 ----------------------- 3 files changed, 1 insertion(+), 154 deletions(-) diff --git a/client/package.json b/client/package.json index 30b23787f5..a0956f0b26 100644 --- a/client/package.json +++ b/client/package.json @@ -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", diff --git a/client/src/utils/handled-error.test.ts b/client/src/utils/handled-error.test.ts index dd96a9783b..79d4ec1ed3 100644 --- a/client/src/utils/handled-error.test.ts +++ b/client/src/utils/handled-error.test.ts @@ -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); - }); - }); }); }); diff --git a/client/src/utils/handled-error.ts b/client/src/utils/handled-error.ts index c1068ca363..b25a6f8c7f 100644 --- a/client/src/utils/handled-error.ts +++ b/client/src/utils/handled-error.ts @@ -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 }; -}