Skip to content

Commit

Permalink
fix: rn error crash
Browse files Browse the repository at this point in the history
  • Loading branch information
sidmorizon committed Nov 22, 2023
1 parent bafb8f6 commit 3e64d4f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
10 changes: 6 additions & 4 deletions packages/core/src/JsBridgeBase.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { CrossEventEmitter } from './CrossEventEmitter';
import isPlainObject from 'lodash/isPlainObject';
import isString from 'lodash/isString';
import { CrossEventEmitter } from './CrossEventEmitter';
import { appDebugLogger, consoleErrorInDev } from './loggers';

import type { Web3ProviderError } from '@onekeyfe/cross-inpage-provider-errors';
import { web3Errors } from '@onekeyfe/cross-inpage-provider-errors';
import {
IDebugLogger,
IInjectedProviderNamesStrings,
IJsBridgeCallback,
IJsBridgeConfig,
IJsBridgeMessagePayload,
IJsBridgeMessageTypes,
IJsonRpcResponse,
IDebugLogger,
} from '@onekeyfe/cross-inpage-provider-types';
import { web3Errors, Web3RpcError } from '@onekeyfe/cross-inpage-provider-errors';
import type { Web3ProviderError } from '@onekeyfe/cross-inpage-provider-errors';
import versionInfo from './versionInfo';

function toPlainError(errorInfo: IErrorInfo) {
Expand All @@ -29,6 +29,7 @@ function toPlainError(errorInfo: IErrorInfo) {
key: errorInfo.key, // i18n key
info: errorInfo.info as unknown, // i18n params
className: errorInfo.className,
autoToast: errorInfo.autoToast,
};
}

Expand Down Expand Up @@ -56,6 +57,7 @@ type IErrorInfo = Error & {
key?: string;
info?: any;
className?: string;
autoToast?: boolean;
};

const BRIDGE_EVENTS = {
Expand Down
32 changes: 11 additions & 21 deletions packages/errors/src/classes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://github.com/MetaMask/rpc-errors/blob/main/src/classes.ts
import safeStringify from 'fast-safe-stringify';

export interface SerializedWeb3RpcError {
Expand All @@ -13,22 +14,16 @@ export interface SerializedWeb3RpcError {
* Permits any integer error code.
*/
export class Web3RpcError<T> extends Error {

public code: number;

public data?: T;

constructor(code: number, message: string, data?: T) {

if (!Number.isInteger(code)) {
throw new Error(
'"code" must be an integer.',
);
throw new Error('"code" must be an integer.');
}
if (!message || typeof message !== 'string') {
throw new Error(
'"message" must be a nonempty string.',
);
throw new Error('"message" must be a nonempty string.');
}

super(message);
Expand All @@ -49,9 +44,12 @@ export class Web3RpcError<T> extends Error {
if (this.data !== undefined) {
serialized.data = this.data;
}
if (this.stack) {
serialized.stack = this.stack;
}

// TODO read error.stack cause RN hermes app crash
// if (this.stack) {
// serialized.stack = this.stack;
// }

return serialized;
}

Expand All @@ -60,11 +58,7 @@ export class Web3RpcError<T> extends Error {
* any circular references.
*/
toString(): string {
return safeStringify(
this.serialize(),
stringifyReplacer,
2,
);
return safeStringify(this.serialize(), stringifyReplacer, 2);
}
}

Expand All @@ -73,17 +67,13 @@ export class Web3RpcError<T> extends Error {
* Permits integer error codes in the [ 1000 <= 4999 ] range.
*/
export class Web3ProviderError<T> extends Web3RpcError<T> {

/**
* Create an Web3 Provider JSON-RPC error.
* `code` must be an integer in the 1000 <= 4999 range.
*/
constructor(code: number, message: string, data?: T) {

if (!isValidWeb3ProviderCode(code)) {
throw new Error(
'"code" must be an integer such that: 1000 <= code <= 4999',
);
throw new Error('"code" must be an integer such that: 1000 <= code <= 4999');
}

super(code, message, data);
Expand Down

0 comments on commit 3e64d4f

Please sign in to comment.