Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
138 lines (109 loc) · 3.74 KB
/
Copy pathutils.js
File metadata and controls
138 lines (109 loc) · 3.74 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const errorValues = require('./errorValues.json')
const FALLBACK_ERROR_CODE = require('./errorCodes.json').rpc.internal
const { EthereumRpcError } = require('./classes')
const JSON_RPC_SERVER_ERROR_MESSAGE = 'Unspecified server error.'
const FALLBACK_MESSAGE = 'Unspecified error message. This is a bug, please report it.'
const FALLBACK_ERROR = {
code: FALLBACK_ERROR_CODE,
message: getMessageFromCode(FALLBACK_ERROR_CODE)
}
/**
* Gets the message for a given code, or a fallback message if the code has
* no corresponding message.
*
* @param {number} code - The integer error code
* @param {string} fallbackMessage - The fallback message
* @return {string} The corresponding message or the fallback message
*/
function getMessageFromCode(code, fallbackMessage = FALLBACK_MESSAGE) {
if (Number.isInteger(code)) {
const codeString = code.toString()
if (errorValues[codeString]) return errorValues[codeString].message
if (isJsonRpcServerError(code)) return JSON_RPC_SERVER_ERROR_MESSAGE
// TODO: allow valid codes and messages to be extended
// // EIP 1193 Status Codes
// if (code >= 4000 && code <= 4999) return Something?
}
return fallbackMessage
}
/**
* Returns whether the given code is valid.
* A code is only valid if it has a message.
*
* @param {number} code - The code to check
* @return {boolean} true if the code is valid, false otherwise.
*/
function isValidCode(code) {
if (!Number.isInteger(code)) return false
const codeString = code.toString()
if (errorValues[codeString]) return true
if (isJsonRpcServerError(code)) return true
// TODO: allow valid codes and messages to be extended
// // EIP 1193 Status Codes
// if (code >= 4000 && code <= 4999) return true
return false
}
/**
* Serializes the given error to an Ethereum JSON RPC-compatible error object.
* Merely copies the given error's values if it is already compatible.
* If the given error is not fully compatible, it will be preserved on the
* returned object's data.originalError property.
* Adds a 'stack' property if it exists on the given error.
*
* @param {any} error - The error to serialize.
* @param {object} fallbackError - The custom fallback error values if the
* given error is invalid.
* @return {object} A standardized error object.
*/
function serializeError (error, fallbackError = FALLBACK_ERROR) {
if (
!fallbackError ||
!Number.isInteger(fallbackError.code) ||
typeof fallbackError.message !== 'string'
) {
throw new Error(
'fallbackError must contain integer number code and string message.'
)
}
if (error instanceof EthereumRpcError) {
return error.serialize()
}
const serialized = {}
if (error && isValidCode(error.code)) {
serialized.code = error.code
if (error.message && typeof error.message === 'string') {
serialized.message = error.message
if (error.hasOwnProperty('data')) serialized.data = error.data
} else {
serialized.message = getMessageFromCode(serialized.code)
serialized.data = { originalError: assignOriginalError(error) }
}
} else {
serialized.code = fallbackError.code
serialized.message = (
error && error.message
? error.message
: fallbackError.message
)
serialized.data = { originalError: assignOriginalError(error) }
}
if (error && error.stack) serialized.stack = error.stack
return serialized
}
// Internal
function isJsonRpcServerError (code) {
return code >= -32099 && code <= -32000
}
function assignOriginalError (error) {
if (error && typeof error === 'object' && !Array.isArray(error)) {
return Object.assign({}, error)
}
return error
}
// Exports
module.exports = {
getMessageFromCode,
isValidCode,
serializeError,
JSON_RPC_SERVER_ERROR_MESSAGE,
}