Skip to content

Commit

Permalink
fix: api error toast (#5906)
Browse files Browse the repository at this point in the history
(cherry picked from commit f209c55)
  • Loading branch information
sidmorizon authored Sep 24, 2024
1 parent 8ca12ac commit 818d6f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
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,
} = 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;
}

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;
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';

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

0 comments on commit 818d6f8

Please sign in to comment.