@onekeyfe/cross-inpage-provider-errors 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,70 @@
1
+ import safeStringify from 'fast-safe-stringify';
2
+ /**
3
+ * Error subclass implementing JSON RPC 2.0 errors and Web3 RPC errors
4
+ * per EIP-1474.
5
+ * Permits any integer error code.
6
+ */
7
+ export class Web3RpcError extends Error {
8
+ constructor(code, message, data) {
9
+ if (!Number.isInteger(code)) {
10
+ throw new Error('"code" must be an integer.');
11
+ }
12
+ if (!message || typeof message !== 'string') {
13
+ throw new Error('"message" must be a nonempty string.');
14
+ }
15
+ super(message);
16
+ this.code = code;
17
+ if (data !== undefined) {
18
+ this.data = data;
19
+ }
20
+ }
21
+ /**
22
+ * Returns a plain object with all public class properties.
23
+ */
24
+ serialize() {
25
+ const serialized = {
26
+ code: this.code,
27
+ message: this.message,
28
+ };
29
+ if (this.data !== undefined) {
30
+ serialized.data = this.data;
31
+ }
32
+ if (this.stack) {
33
+ serialized.stack = this.stack;
34
+ }
35
+ return serialized;
36
+ }
37
+ /**
38
+ * Return a string representation of the serialized error, omitting
39
+ * any circular references.
40
+ */
41
+ toString() {
42
+ return safeStringify(this.serialize(), stringifyReplacer, 2);
43
+ }
44
+ }
45
+ /**
46
+ * Error subclass implementing Web3 Provider errors per EIP-1193.
47
+ * Permits integer error codes in the [ 1000 <= 4999 ] range.
48
+ */
49
+ export class Web3ProviderError extends Web3RpcError {
50
+ /**
51
+ * Create an Web3 Provider JSON-RPC error.
52
+ * `code` must be an integer in the 1000 <= 4999 range.
53
+ */
54
+ constructor(code, message, data) {
55
+ if (!isValidWeb3ProviderCode(code)) {
56
+ throw new Error('"code" must be an integer such that: 1000 <= code <= 4999');
57
+ }
58
+ super(code, message, data);
59
+ }
60
+ }
61
+ // Internal
62
+ function isValidWeb3ProviderCode(code) {
63
+ return Number.isInteger(code) && code >= 1000 && code <= 4999;
64
+ }
65
+ function stringifyReplacer(_, value) {
66
+ if (value === '[Circular]') {
67
+ return undefined;
68
+ }
69
+ return value;
70
+ }
@@ -0,0 +1,95 @@
1
+ interface ErrorCodes {
2
+ readonly rpc: {
3
+ readonly invalidInput: -32000;
4
+ readonly resourceNotFound: -32001;
5
+ readonly resourceUnavailable: -32002;
6
+ readonly transactionRejected: -32003;
7
+ readonly methodNotSupported: -32004;
8
+ readonly limitExceeded: -32005;
9
+ readonly parse: -32700;
10
+ readonly invalidRequest: -32600;
11
+ readonly methodNotFound: -32601;
12
+ readonly invalidParams: -32602;
13
+ readonly internal: -32603;
14
+ };
15
+ readonly provider: {
16
+ readonly userRejectedRequest: 4001;
17
+ readonly unauthorized: 4100;
18
+ readonly unsupportedMethod: 4200;
19
+ readonly disconnected: 4900;
20
+ readonly chainDisconnected: 4901;
21
+ readonly requestTimeout: 4500;
22
+ };
23
+ }
24
+ export declare const errorCodes: ErrorCodes;
25
+ export declare const errorValues: {
26
+ '-32700': {
27
+ standard: string;
28
+ message: string;
29
+ };
30
+ '-32600': {
31
+ standard: string;
32
+ message: string;
33
+ };
34
+ '-32601': {
35
+ standard: string;
36
+ message: string;
37
+ };
38
+ '-32602': {
39
+ standard: string;
40
+ message: string;
41
+ };
42
+ '-32603': {
43
+ standard: string;
44
+ message: string;
45
+ };
46
+ '-32000': {
47
+ standard: string;
48
+ message: string;
49
+ };
50
+ '-32001': {
51
+ standard: string;
52
+ message: string;
53
+ };
54
+ '-32002': {
55
+ standard: string;
56
+ message: string;
57
+ };
58
+ '-32003': {
59
+ standard: string;
60
+ message: string;
61
+ };
62
+ '-32004': {
63
+ standard: string;
64
+ message: string;
65
+ };
66
+ '-32005': {
67
+ standard: string;
68
+ message: string;
69
+ };
70
+ '4001': {
71
+ standard: string;
72
+ message: string;
73
+ };
74
+ '4100': {
75
+ standard: string;
76
+ message: string;
77
+ };
78
+ '4200': {
79
+ standard: string;
80
+ message: string;
81
+ };
82
+ '4900': {
83
+ standard: string;
84
+ message: string;
85
+ };
86
+ '4901': {
87
+ standard: string;
88
+ message: string;
89
+ };
90
+ '4500': {
91
+ standard: string;
92
+ message: string;
93
+ };
94
+ };
95
+ export {};
@@ -0,0 +1,95 @@
1
+ export const errorCodes = {
2
+ rpc: {
3
+ invalidInput: -32000,
4
+ resourceNotFound: -32001,
5
+ resourceUnavailable: -32002,
6
+ transactionRejected: -32003,
7
+ methodNotSupported: -32004,
8
+ limitExceeded: -32005,
9
+ parse: -32700,
10
+ invalidRequest: -32600,
11
+ methodNotFound: -32601,
12
+ invalidParams: -32602,
13
+ internal: -32603,
14
+ },
15
+ provider: {
16
+ userRejectedRequest: 4001,
17
+ unauthorized: 4100,
18
+ unsupportedMethod: 4200,
19
+ disconnected: 4900,
20
+ chainDisconnected: 4901,
21
+ requestTimeout: 4500,
22
+ },
23
+ };
24
+ export const errorValues = {
25
+ '-32700': {
26
+ standard: 'JSON RPC 2.0',
27
+ message: 'Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.',
28
+ },
29
+ '-32600': {
30
+ standard: 'JSON RPC 2.0',
31
+ message: 'The JSON sent is not a valid Request object.',
32
+ },
33
+ '-32601': {
34
+ standard: 'JSON RPC 2.0',
35
+ message: 'The method does not exist / is not available.',
36
+ },
37
+ '-32602': {
38
+ standard: 'JSON RPC 2.0',
39
+ message: 'Invalid method parameter(s).',
40
+ },
41
+ '-32603': {
42
+ standard: 'JSON RPC 2.0',
43
+ message: 'Internal JSON-RPC error.',
44
+ },
45
+ '-32000': {
46
+ standard: 'EIP-1474',
47
+ message: 'Invalid input.',
48
+ },
49
+ '-32001': {
50
+ standard: 'EIP-1474',
51
+ message: 'Resource not found.',
52
+ },
53
+ '-32002': {
54
+ standard: 'EIP-1474',
55
+ message: 'Resource unavailable.',
56
+ },
57
+ '-32003': {
58
+ standard: 'EIP-1474',
59
+ message: 'Transaction rejected.',
60
+ },
61
+ '-32004': {
62
+ standard: 'EIP-1474',
63
+ message: 'Method not supported.',
64
+ },
65
+ '-32005': {
66
+ standard: 'EIP-1474',
67
+ message: 'Request limit exceeded.',
68
+ },
69
+ '4001': {
70
+ standard: 'EIP-1193',
71
+ message: 'User rejected the request.',
72
+ },
73
+ '4100': {
74
+ standard: 'EIP-1193',
75
+ message: 'The requested account and/or method has not been authorized by the user.',
76
+ },
77
+ '4200': {
78
+ standard: 'EIP-1193',
79
+ message: 'The requested method is not supported by this Web3 provider.',
80
+ },
81
+ '4900': {
82
+ standard: 'EIP-1193',
83
+ message: 'The provider is disconnected from all chains.',
84
+ },
85
+ '4901': {
86
+ standard: 'EIP-1193',
87
+ message: 'The provider is disconnected from the specified chain.',
88
+ },
89
+ '4500': {
90
+ standard: '',
91
+ message: 'The request by this Web3 provider is timeout.',
92
+ },
93
+ };
94
+ // https://eips.ethereum.org/EIPS/eip-1474#error-codes
95
+ // https://eips.ethereum.org/EIPS/eip-1193#provider-errors
@@ -0,0 +1,92 @@
1
+ import { Web3RpcError, Web3ProviderError } from './classes';
2
+ interface Web3ErrorOptions<T> {
3
+ message?: string;
4
+ data?: T;
5
+ }
6
+ interface ServerErrorOptions<T> extends Web3ErrorOptions<T> {
7
+ code: number;
8
+ }
9
+ declare type CustomErrorArg<T> = ServerErrorOptions<T>;
10
+ declare type Web3ErrorsArg<T> = Web3ErrorOptions<T> | string;
11
+ export declare const web3Errors: {
12
+ rpc: {
13
+ /**
14
+ * Get a JSON RPC 2.0 Parse (-32700) error.
15
+ */
16
+ parse: <T>(arg?: Web3ErrorsArg<T> | undefined) => Web3RpcError<T>;
17
+ /**
18
+ * Get a JSON RPC 2.0 Invalid Request (-32600) error.
19
+ */
20
+ invalidRequest: <T_1>(arg?: Web3ErrorsArg<T_1> | undefined) => Web3RpcError<T_1>;
21
+ /**
22
+ * Get a JSON RPC 2.0 Invalid Params (-32602) error.
23
+ */
24
+ invalidParams: <T_2>(arg?: Web3ErrorsArg<T_2> | undefined) => Web3RpcError<T_2>;
25
+ /**
26
+ * Get a JSON RPC 2.0 Method Not Found (-32601) error.
27
+ */
28
+ methodNotFound: <T_3>(arg?: Web3ErrorsArg<T_3> | undefined) => Web3RpcError<T_3>;
29
+ /**
30
+ * Get a JSON RPC 2.0 Internal (-32603) error.
31
+ */
32
+ internal: <T_4>(arg?: Web3ErrorsArg<T_4> | undefined) => Web3RpcError<T_4>;
33
+ /**
34
+ * Get a JSON RPC 2.0 Server error.
35
+ * Permits integer error codes in the [ -32099 <= -32005 ] range.
36
+ * Codes -32000 through -32004 are reserved by EIP-1474.
37
+ */
38
+ server: <T_5>(opts: ServerErrorOptions<T_5>) => Web3RpcError<T_5>;
39
+ /**
40
+ * Get an Web3 JSON RPC Invalid Input (-32000) error.
41
+ */
42
+ invalidInput: <T_6>(arg?: Web3ErrorsArg<T_6> | undefined) => Web3RpcError<T_6>;
43
+ /**
44
+ * Get an Web3 JSON RPC Resource Not Found (-32001) error.
45
+ */
46
+ resourceNotFound: <T_7>(arg?: Web3ErrorsArg<T_7> | undefined) => Web3RpcError<T_7>;
47
+ /**
48
+ * Get an Web3 JSON RPC Resource Unavailable (-32002) error.
49
+ */
50
+ resourceUnavailable: <T_8>(arg?: Web3ErrorsArg<T_8> | undefined) => Web3RpcError<T_8>;
51
+ /**
52
+ * Get an Web3 JSON RPC Transaction Rejected (-32003) error.
53
+ */
54
+ transactionRejected: <T_9>(arg?: Web3ErrorsArg<T_9> | undefined) => Web3RpcError<T_9>;
55
+ /**
56
+ * Get an Web3 JSON RPC Method Not Supported (-32004) error.
57
+ */
58
+ methodNotSupported: <T_10>(arg?: Web3ErrorsArg<T_10> | undefined) => Web3RpcError<T_10>;
59
+ /**
60
+ * Get an Web3 JSON RPC Limit Exceeded (-32005) error.
61
+ */
62
+ limitExceeded: <T_11>(arg?: Web3ErrorsArg<T_11> | undefined) => Web3RpcError<T_11>;
63
+ };
64
+ provider: {
65
+ /**
66
+ * Get an Web3 Provider User Rejected Request (4001) error.
67
+ */
68
+ userRejectedRequest: <T_12>(arg?: Web3ErrorsArg<T_12> | undefined) => Web3ProviderError<T_12>;
69
+ /**
70
+ * Get an Web3 Provider Unauthorized (4100) error.
71
+ */
72
+ unauthorized: <T_13>(arg?: Web3ErrorsArg<T_13> | undefined) => Web3ProviderError<T_13>;
73
+ /**
74
+ * Get an Web3 Provider Unsupported Method (4200) error.
75
+ */
76
+ unsupportedMethod: <T_14>(arg?: Web3ErrorsArg<T_14> | undefined) => Web3ProviderError<T_14>;
77
+ /**
78
+ * Get an Web3 Provider Not Connected (4900) error.
79
+ */
80
+ disconnected: <T_15>(arg?: Web3ErrorsArg<T_15> | undefined) => Web3ProviderError<T_15>;
81
+ /**
82
+ * Get an Web3 Provider Chain Not Connected (4901) error.
83
+ */
84
+ chainDisconnected: <T_16>(arg?: Web3ErrorsArg<T_16> | undefined) => Web3ProviderError<T_16>;
85
+ requestTimeout: <T_17>(arg?: Web3ErrorsArg<T_17> | undefined) => Web3ProviderError<T_17>;
86
+ /**
87
+ * Get a custom Web3 Provider error.
88
+ */
89
+ custom: <T_18>(opts: CustomErrorArg<T_18>) => Web3ProviderError<T_18>;
90
+ };
91
+ };
92
+ export {};
package/dist/errors.js ADDED
@@ -0,0 +1,138 @@
1
+ import { Web3RpcError, Web3ProviderError } from './classes';
2
+ import { getMessageFromCode } from './utils';
3
+ import { errorCodes } from './error-constants';
4
+ export const web3Errors = {
5
+ rpc: {
6
+ /**
7
+ * Get a JSON RPC 2.0 Parse (-32700) error.
8
+ */
9
+ parse: (arg) => getWeb3JsonRpcError(errorCodes.rpc.parse, arg),
10
+ /**
11
+ * Get a JSON RPC 2.0 Invalid Request (-32600) error.
12
+ */
13
+ invalidRequest: (arg) => getWeb3JsonRpcError(errorCodes.rpc.invalidRequest, arg),
14
+ /**
15
+ * Get a JSON RPC 2.0 Invalid Params (-32602) error.
16
+ */
17
+ invalidParams: (arg) => getWeb3JsonRpcError(errorCodes.rpc.invalidParams, arg),
18
+ /**
19
+ * Get a JSON RPC 2.0 Method Not Found (-32601) error.
20
+ */
21
+ methodNotFound: (arg) => getWeb3JsonRpcError(errorCodes.rpc.methodNotFound, arg),
22
+ /**
23
+ * Get a JSON RPC 2.0 Internal (-32603) error.
24
+ */
25
+ internal: (arg) => getWeb3JsonRpcError(errorCodes.rpc.internal, arg),
26
+ /**
27
+ * Get a JSON RPC 2.0 Server error.
28
+ * Permits integer error codes in the [ -32099 <= -32005 ] range.
29
+ * Codes -32000 through -32004 are reserved by EIP-1474.
30
+ */
31
+ server: (opts) => {
32
+ if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
33
+ throw new Error('Web3 RPC Server errors must provide single object argument.');
34
+ }
35
+ const { code } = opts;
36
+ if (!Number.isInteger(code) || code > -32005 || code < -32099) {
37
+ throw new Error('"code" must be an integer such that: -32099 <= code <= -32005');
38
+ }
39
+ return getWeb3JsonRpcError(code, opts);
40
+ },
41
+ /**
42
+ * Get an Web3 JSON RPC Invalid Input (-32000) error.
43
+ */
44
+ invalidInput: (arg) => getWeb3JsonRpcError(errorCodes.rpc.invalidInput, arg),
45
+ /**
46
+ * Get an Web3 JSON RPC Resource Not Found (-32001) error.
47
+ */
48
+ resourceNotFound: (arg) => getWeb3JsonRpcError(errorCodes.rpc.resourceNotFound, arg),
49
+ /**
50
+ * Get an Web3 JSON RPC Resource Unavailable (-32002) error.
51
+ */
52
+ resourceUnavailable: (arg) => getWeb3JsonRpcError(errorCodes.rpc.resourceUnavailable, arg),
53
+ /**
54
+ * Get an Web3 JSON RPC Transaction Rejected (-32003) error.
55
+ */
56
+ transactionRejected: (arg) => getWeb3JsonRpcError(errorCodes.rpc.transactionRejected, arg),
57
+ /**
58
+ * Get an Web3 JSON RPC Method Not Supported (-32004) error.
59
+ */
60
+ methodNotSupported: (arg) => getWeb3JsonRpcError(errorCodes.rpc.methodNotSupported, arg),
61
+ /**
62
+ * Get an Web3 JSON RPC Limit Exceeded (-32005) error.
63
+ */
64
+ limitExceeded: (arg) => getWeb3JsonRpcError(errorCodes.rpc.limitExceeded, arg),
65
+ },
66
+ provider: {
67
+ /**
68
+ * Get an Web3 Provider User Rejected Request (4001) error.
69
+ */
70
+ userRejectedRequest: (arg) => {
71
+ return getWeb3ProviderError(errorCodes.provider.userRejectedRequest, arg);
72
+ },
73
+ /**
74
+ * Get an Web3 Provider Unauthorized (4100) error.
75
+ */
76
+ unauthorized: (arg) => {
77
+ return getWeb3ProviderError(errorCodes.provider.unauthorized, arg);
78
+ },
79
+ /**
80
+ * Get an Web3 Provider Unsupported Method (4200) error.
81
+ */
82
+ unsupportedMethod: (arg) => {
83
+ return getWeb3ProviderError(errorCodes.provider.unsupportedMethod, arg);
84
+ },
85
+ /**
86
+ * Get an Web3 Provider Not Connected (4900) error.
87
+ */
88
+ disconnected: (arg) => {
89
+ return getWeb3ProviderError(errorCodes.provider.disconnected, arg);
90
+ },
91
+ /**
92
+ * Get an Web3 Provider Chain Not Connected (4901) error.
93
+ */
94
+ chainDisconnected: (arg) => {
95
+ return getWeb3ProviderError(errorCodes.provider.chainDisconnected, arg);
96
+ },
97
+ requestTimeout: (arg) => {
98
+ return getWeb3ProviderError(errorCodes.provider.requestTimeout, arg);
99
+ },
100
+ /**
101
+ * Get a custom Web3 Provider error.
102
+ */
103
+ custom: (opts) => {
104
+ if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
105
+ throw new Error('Web3 Provider custom errors must provide single object argument.');
106
+ }
107
+ const { code, message, data } = opts;
108
+ if (!message || typeof message !== 'string') {
109
+ throw new Error('"message" must be a nonempty string');
110
+ }
111
+ return new Web3ProviderError(code, message, data);
112
+ },
113
+ },
114
+ };
115
+ // Internal
116
+ function getWeb3JsonRpcError(code, arg) {
117
+ const [message, data] = parseOpts(arg);
118
+ return new Web3RpcError(code, message || getMessageFromCode(code), data);
119
+ }
120
+ function getWeb3ProviderError(code, arg) {
121
+ const [message, data] = parseOpts(arg);
122
+ return new Web3ProviderError(code, message || getMessageFromCode(code), data);
123
+ }
124
+ function parseOpts(arg) {
125
+ if (arg) {
126
+ if (typeof arg === 'string') {
127
+ return [arg];
128
+ }
129
+ else if (typeof arg === 'object' && !Array.isArray(arg)) {
130
+ const { message, data } = arg;
131
+ if (message && typeof message !== 'string') {
132
+ throw new Error('Must specify string message.');
133
+ }
134
+ return [message || undefined, data];
135
+ }
136
+ }
137
+ return [];
138
+ }
@@ -0,0 +1,5 @@
1
+ import { Web3RpcError, Web3ProviderError } from './classes';
2
+ import { serializeError, getMessageFromCode } from './utils';
3
+ import { web3Errors } from './errors';
4
+ import { errorCodes } from './error-constants';
5
+ export { errorCodes, web3Errors, Web3RpcError, Web3ProviderError, serializeError, getMessageFromCode, };
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import { Web3RpcError, Web3ProviderError } from './classes';
2
+ import { serializeError, getMessageFromCode, } from './utils';
3
+ import { web3Errors } from './errors';
4
+ import { errorCodes } from './error-constants';
5
+ export { errorCodes, web3Errors, Web3RpcError, Web3ProviderError, serializeError, getMessageFromCode, };
@@ -0,0 +1,22 @@
1
+ import { SerializedWeb3RpcError } from './classes';
2
+ export declare const JSON_RPC_SERVER_ERROR_MESSAGE = "Unspecified server error.";
3
+ /**
4
+ * Gets the message for a given code, or a fallback message if the code has
5
+ * no corresponding message.
6
+ */
7
+ export declare function getMessageFromCode(code: number, fallbackMessage?: string): string;
8
+ /**
9
+ * Returns whether the given code is valid.
10
+ * A code is only valid if it has a message.
11
+ */
12
+ export declare function isValidCode(code: number): boolean;
13
+ /**
14
+ * Serializes the given error to an Web3 JSON RPC-compatible error object.
15
+ * Merely copies the given error's values if it is already compatible.
16
+ * If the given error is not fully compatible, it will be preserved on the
17
+ * returned object's data.originalError property.
18
+ */
19
+ export declare function serializeError(error: unknown, { fallbackError, shouldIncludeStack, }?: {
20
+ fallbackError?: SerializedWeb3RpcError | undefined;
21
+ shouldIncludeStack?: boolean | undefined;
22
+ }): SerializedWeb3RpcError;
package/dist/utils.js ADDED
@@ -0,0 +1,104 @@
1
+ import { errorCodes, errorValues } from './error-constants';
2
+ import { Web3RpcError } from './classes';
3
+ const FALLBACK_ERROR_CODE = errorCodes.rpc.internal;
4
+ const FALLBACK_MESSAGE = 'Unspecified error message. This is a bug, please report it.';
5
+ const FALLBACK_ERROR = {
6
+ code: FALLBACK_ERROR_CODE,
7
+ message: getMessageFromCode(FALLBACK_ERROR_CODE),
8
+ };
9
+ export const JSON_RPC_SERVER_ERROR_MESSAGE = 'Unspecified server error.';
10
+ /**
11
+ * Gets the message for a given code, or a fallback message if the code has
12
+ * no corresponding message.
13
+ */
14
+ export function getMessageFromCode(code, fallbackMessage = FALLBACK_MESSAGE) {
15
+ if (Number.isInteger(code)) {
16
+ const codeString = code.toString();
17
+ if (hasKey(errorValues, codeString)) {
18
+ return errorValues[codeString].message;
19
+ }
20
+ if (isJsonRpcServerError(code)) {
21
+ return JSON_RPC_SERVER_ERROR_MESSAGE;
22
+ }
23
+ }
24
+ return fallbackMessage;
25
+ }
26
+ /**
27
+ * Returns whether the given code is valid.
28
+ * A code is only valid if it has a message.
29
+ */
30
+ export function isValidCode(code) {
31
+ if (!Number.isInteger(code)) {
32
+ return false;
33
+ }
34
+ const codeString = code.toString();
35
+ if (errorValues[codeString]) {
36
+ return true;
37
+ }
38
+ if (isJsonRpcServerError(code)) {
39
+ return true;
40
+ }
41
+ return false;
42
+ }
43
+ /**
44
+ * Serializes the given error to an Web3 JSON RPC-compatible error object.
45
+ * Merely copies the given error's values if it is already compatible.
46
+ * If the given error is not fully compatible, it will be preserved on the
47
+ * returned object's data.originalError property.
48
+ */
49
+ export function serializeError(error, { fallbackError = FALLBACK_ERROR, shouldIncludeStack = false, } = {}) {
50
+ var _a, _b;
51
+ if (!fallbackError ||
52
+ !Number.isInteger(fallbackError.code) ||
53
+ typeof fallbackError.message !== 'string') {
54
+ throw new Error('Must provide fallback error with integer number code and string message.');
55
+ }
56
+ if (error instanceof Web3RpcError) {
57
+ return error.serialize();
58
+ }
59
+ const serialized = {};
60
+ if (error &&
61
+ typeof error === 'object' &&
62
+ !Array.isArray(error) &&
63
+ hasKey(error, 'code') &&
64
+ isValidCode(error.code)) {
65
+ const _error = error;
66
+ serialized.code = _error.code;
67
+ if (_error.message && typeof _error.message === 'string') {
68
+ serialized.message = _error.message;
69
+ if (hasKey(_error, 'data')) {
70
+ serialized.data = _error.data;
71
+ }
72
+ }
73
+ else {
74
+ serialized.message = getMessageFromCode(serialized.code);
75
+ serialized.data = { originalError: assignOriginalError(error) };
76
+ }
77
+ }
78
+ else {
79
+ serialized.code = fallbackError.code;
80
+ const message = (_a = error) === null || _a === void 0 ? void 0 : _a.message;
81
+ serialized.message = (message && typeof message === 'string'
82
+ ? message
83
+ : fallbackError.message);
84
+ serialized.data = { originalError: assignOriginalError(error) };
85
+ }
86
+ const stack = (_b = error) === null || _b === void 0 ? void 0 : _b.stack;
87
+ if (shouldIncludeStack && error && stack && typeof stack === 'string') {
88
+ serialized.stack = stack;
89
+ }
90
+ return serialized;
91
+ }
92
+ // Internal
93
+ function isJsonRpcServerError(code) {
94
+ return code >= -32099 && code <= -32000;
95
+ }
96
+ function assignOriginalError(error) {
97
+ if (error && typeof error === 'object' && !Array.isArray(error)) {
98
+ return Object.assign({}, error);
99
+ }
100
+ return error;
101
+ }
102
+ function hasKey(obj, key) {
103
+ return Object.prototype.hasOwnProperty.call(obj, key);
104
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@onekeyfe/cross-inpage-provider-errors",
3
+ "version": "0.0.2",
4
+ "keywords": [
5
+ "cross-inpage-provider"
6
+ ],
7
+ "author": "dev-fe@onekey.so",
8
+ "repository": "https://github.com/OneKeyHQ/cross-inpage-provider",
9
+ "license": "Apache-2.0",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "type": "module",
14
+ "files": [
15
+ "dist/*"
16
+ ],
17
+ "exports": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/cjs/index.js"
21
+ },
22
+ "types": "./dist/index.d.ts",
23
+ "module": "./dist/index.js",
24
+ "main": "./dist/cjs/index.js",
25
+ "scripts": {
26
+ "prebuild": "rm -rf dist",
27
+ "build": "tsc && tsc --project tsconfig.cjs.json",
28
+ "start": "tsc --watch"
29
+ },
30
+ "dependencies": {
31
+ "fast-safe-stringify": "^2.1.1"
32
+ },
33
+ "gitHead": "97aa3c911466f5c3a46b005a69d5b3edd1d8fd7c"
34
+ }