Skip to content

Commit

Permalink
fix: axios network i18n (#5878)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidmorizon authored Sep 20, 2024
1 parent da24870 commit 85253c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/shared/src/errors/errors/baseErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -67,6 +70,8 @@ export class OneKeyError<
autoToast,
requestId,
payload: hardwareErrorPayload,
className,
name,
} = errorProps);
} else {
msg = isString(errorProps) ? errorProps : '';
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/errors/types/errorTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum EOneKeyErrorClassNames {
OneKeyValidatorTip = 'OneKeyValidatorTip',
OneKeyAbortError = 'OneKeyAbortError',
AxiosAbortCancelError = 'AxiosAbortCancelError',
AxiosNetworkError = 'AxiosNetworkError',
OneKeyWalletConnectModalCloseError = 'OneKeyWalletConnectModalCloseError',
OneKeyAlreadyExistWalletError = 'OneKeyAlreadyExistWalletError',
PasswordPromptDialogCancel = 'PasswordPromptDialogCancel',
Expand Down
22 changes: 20 additions & 2 deletions packages/shared/src/request/axiosInterceptor.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
},
);
Expand Down

0 comments on commit 85253c7

Please sign in to comment.