Repository navigation
Expand file tree
/
Copy pathclasses.js
More file actions
117 lines (101 loc) · 2.46 KB
/
Copy pathclasses.js
File metadata and controls
117 lines (101 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const safeStringify = require('fast-safe-stringify')
/**
* @class JsonRpcError
* Error subclass implementing JSON RPC 2.0 errors and Ethereum RPC errors
* per EIP 1474.
* Permits any integer error code.
*/
class EthereumRpcError extends Error {
/**
* Create an Ethereum JSON RPC error.
*
* @param {number} code - The integer error code.
* @param {string} message - The string message.
* @param {any} [data] - The error data.
*/
constructor (code, message, data) {
if (!Number.isInteger(code)) {
throw new Error(
'"code" must be an integer.',
)
}
if (!message || typeof message !== 'string') {
throw new Error(
'"message" must be a nonempty string.',
)
}
super(message)
this.code = code
if (data !== undefined) {
this.data = data
}
}
/**
* Returns a plain object with all public class properties.
*
* @returns {object} The serialized error.
*/
serialize () {
const serialized = {
code: this.code,
message: this.message,
}
if (this.data !== undefined) {
serialized.data = this.data
}
if (this.stack) {
serialized.stack = this.stack
}
return serialized
}
/**
* Return a string representation of the serialized error, omitting
* any circular references.
*
* @returns {string} The serialized error as a string.
*/
toString () {
return safeStringify(
this.serialize(),
stringifyReplacer,
2,
)
}
}
/**
* @class EthereumRpcError
* Error subclass implementing Ethereum Provider errors per EIP 1193.
* Permits integer error codes in the [ 1000 <= 4999 ] range.
*/
class EthereumProviderError extends EthereumRpcError {
/**
* Create an Ethereum JSON RPC error.
*
* @param {number} code - The integer error code, in the [ 1000 <= 4999 ] range.
* @param {string} message - The string message.
* @param {any} [data] - The error data.
*/
constructor (code, message, data) {
if (!isValidEthProviderCode(code)) {
throw new Error(
'"code" must be an integer such that: 1000 <= code <= 4999',
)
}
super(code, message, data)
}
}
// Internal
function isValidEthProviderCode (code) {
return Number.isInteger(code) && code >= 1000 && code <= 4999
}
function stringifyReplacer (_, value) {
if (value === '[Circular]') {
return undefined
}
return value
}
// Exports
module.exports = {
EthereumRpcError,
EthereumProviderError,
}