From 85253c70ae36bc8226a34b5596c25cde2de02c88 Mon Sep 17 00:00:00 2001 From: morizon Date: Fri, 20 Sep 2024 18:40:58 +0800 Subject: [PATCH] fix: axios network i18n (#5878) --- .../shared/src/errors/errors/baseErrors.ts | 11 ++++++++++ .../shared/src/errors/types/errorTypes.ts | 1 + .../shared/src/request/axiosInterceptor.ts | 22 +++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/errors/errors/baseErrors.ts b/packages/shared/src/errors/errors/baseErrors.ts index d4a324b3e87..0609dd4e55f 100644 --- a/packages/shared/src/errors/errors/baseErrors.ts +++ b/packages/shared/src/errors/errors/baseErrors.ts @@ -57,6 +57,9 @@ export class OneKeyError< let hardwareErrorPayload: IOneKeyHardwareErrorPayload | undefined; let autoToast: boolean | undefined; let requestId: string | undefined; + let className: EOneKeyErrorClassNames | undefined; + let name: string | undefined; + if (!isString(errorProps) && errorProps && isObject(errorProps)) { ({ message: msg, @@ -67,6 +70,8 @@ export class OneKeyError< autoToast, requestId, payload: hardwareErrorPayload, + className, + name, } = errorProps); } else { msg = isString(errorProps) ? errorProps : ''; @@ -93,6 +98,12 @@ export class OneKeyError< } this.autoToast = autoToast; this.requestId = requestId; + if (className) { + this.className = className; + } + if (name) { + this.name = name; + } } // for jest only: this is not stable, do not use it. may be different in compressed code diff --git a/packages/shared/src/errors/types/errorTypes.ts b/packages/shared/src/errors/types/errorTypes.ts index 0ac330e07d3..7f2fe6dcb91 100644 --- a/packages/shared/src/errors/types/errorTypes.ts +++ b/packages/shared/src/errors/types/errorTypes.ts @@ -22,6 +22,7 @@ export enum EOneKeyErrorClassNames { OneKeyValidatorTip = 'OneKeyValidatorTip', OneKeyAbortError = 'OneKeyAbortError', AxiosAbortCancelError = 'AxiosAbortCancelError', + AxiosNetworkError = 'AxiosNetworkError', OneKeyWalletConnectModalCloseError = 'OneKeyWalletConnectModalCloseError', OneKeyAlreadyExistWalletError = 'OneKeyAlreadyExistWalletError', PasswordPromptDialogCancel = 'PasswordPromptDialogCancel', diff --git a/packages/shared/src/request/axiosInterceptor.ts b/packages/shared/src/request/axiosInterceptor.ts index 1b8207b4086..324421bf4c5 100644 --- a/packages/shared/src/request/axiosInterceptor.ts +++ b/packages/shared/src/request/axiosInterceptor.ts @@ -1,12 +1,13 @@ /* eslint-disable @typescript-eslint/no-restricted-imports */ /* eslint-disable @typescript-eslint/unbound-method */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ -import axios from 'axios'; +import axios, { AxiosError } from 'axios'; import { forEach } from 'lodash'; -import { OneKeyServerApiError } from '@onekeyhq/shared/src/errors'; +import { OneKeyError, OneKeyServerApiError } from '@onekeyhq/shared/src/errors'; import type { IOneKeyAPIBaseResponse } from '@onekeyhq/shared/types/request'; +import { EOneKeyErrorClassNames } from '../errors/types/errorTypes'; import { ETranslations } from '../locale'; import { appLocale } from '../locale/appLocale'; import { defaultLogger } from '../logger/logger'; @@ -128,6 +129,23 @@ axios.interceptors.response.use( }); } } + if ( + error && + error instanceof AxiosError && + error.message === 'Network Error' && + error.code === AxiosError.ERR_NETWORK && + error.name === 'AxiosError' + ) { + const title = appLocale.intl.formatMessage({ + id: ETranslations.global_network_error, + }); + throw new OneKeyError({ + name: error.name, + message: title, + className: EOneKeyErrorClassNames.AxiosNetworkError, + key: ETranslations.global_network_error, + }); + } throw error; }, );