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: api error toast #5908

Merged
merged 7 commits into from
Sep 26, 2024
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 @@ -23,7 +23,7 @@ export function ErrorToastContainer() {
let toastId = isFilterErrorCode(p.errorCode)
? String(p.errorCode)
: undefined;
toastId = toastId || message;
toastId = toastId || (p.title ? p.title : message);
const actions = isRequestIdMessage(message) ? (
<Button
size="small"
Expand Down
8 changes: 7 additions & 1 deletion packages/shared/src/errors/errors/baseErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class OneKeyError<
let requestId: string | undefined;
let className: EOneKeyErrorClassNames | undefined;
let name: string | undefined;
let disableFallbackMessage: boolean | undefined;

if (!isString(errorProps) && errorProps && isObject(errorProps)) {
({
Expand All @@ -72,6 +73,7 @@ export class OneKeyError<
payload: hardwareErrorPayload,
className,
name,
disableFallbackMessage,
originalix marked this conversation as resolved.
Show resolved Hide resolved
} = errorProps);
} else {
msg = isString(errorProps) ? errorProps : '';
Expand All @@ -83,7 +85,11 @@ export class OneKeyError<
// * empty string not allowed in Web3RpcError, give a fakeMessage by default
// * can not access this.key before constructor
msg ||
`Unknown Onekey Internal Error. ${[key].filter(Boolean).join(':')}`,
(disableFallbackMessage
? ''
: `Unknown Onekey Internal Error. ${[key]
.filter(Boolean)
.join(':')}`),
data,
);

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 @@ -77,6 +77,7 @@ export interface IOneKeyError<

// ---server props
requestId?: string;
disableFallbackMessage?: boolean;
originalix marked this conversation as resolved.
Show resolved Hide resolved
}

export type IOneKeyHardwareErrorPayload = {
Expand Down
16 changes: 14 additions & 2 deletions packages/shared/src/errors/utils/errorToastUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { HardwareErrorCode } from '@onekeyfe/hd-shared';
import axios from 'axios';
import { isPlainObject } from 'lodash';

Expand Down Expand Up @@ -34,10 +33,23 @@ function showToastOfError(error: IOneKeyError | unknown | undefined) {
) {
return;
}
let shouldMuteToast = false;
if (
err?.className === EOneKeyErrorClassNames.OneKeyServerApiError &&
!err?.message
) {
shouldMuteToast = true;
}
const isTriggered = err?.$$autoToastErrorTriggered;
const isSameError = lastToastErrorInstance === err;
// TODO log error to file if developer mode on
if (err && err?.autoToast && !isTriggered && !isSameError) {
if (
err &&
err?.autoToast &&
!isTriggered &&
!isSameError &&
!shouldMuteToast
) {
err.$$autoToastErrorTriggered = true;
originalix marked this conversation as resolved.
Show resolved Hide resolved
lastToastErrorInstance = err;
appEventBus.emit(EAppEventBusNames.ShowToast, {
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/request/axiosInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ axios.interceptors.response.use(

const data = response.data as IOneKeyAPIBaseResponse;

// test code
// data.code = 4485;
// data.message = 'hhhh';

originalix marked this conversation as resolved.
Show resolved Hide resolved
if (data.code !== 0) {
const requestIdKey = HEADER_REQUEST_ID_KEY;
if (platformEnv.isDev) {
Expand All @@ -81,6 +85,7 @@ axios.interceptors.response.use(

throw new OneKeyServerApiError({
autoToast: true,
disableFallbackMessage: true,
message: data.message,
code: data.code,
data,
Expand Down
Loading