@@ -13,19 +13,24 @@ const {
13
13
ObjectGetOwnPropertyNames,
14
14
ObjectGetPrototypeOf,
15
15
ObjectKeys,
16
+ ObjectPrototypeHasOwnProperty,
16
17
ObjectPrototypeToString,
17
18
RangeError,
18
19
ReferenceError,
19
20
SafeSet,
20
21
SymbolToStringTag,
21
22
SyntaxError,
23
+ SymbolFor,
22
24
TypeError,
23
25
URIError,
24
26
} = primordials ;
27
+ const { inspect : { custom : customInspectSymbol } } = require ( 'util' ) ;
25
28
26
29
const kSerializedError = 0 ;
27
30
const kSerializedObject = 1 ;
28
31
const kInspectedError = 2 ;
32
+ const kInspectedSymbol = 3 ;
33
+ const kCustomInspecteObject = 4 ;
29
34
30
35
const errors = {
31
36
Error, TypeError, RangeError, URIError, SyntaxError, ReferenceError, EvalError,
@@ -52,7 +57,13 @@ function TryGetAllProperties(object, target = object) {
52
57
// Continue regardless of error.
53
58
}
54
59
}
55
- if ( 'value' in descriptor && typeof descriptor . value !== 'function' ) {
60
+ if ( key === 'cause' ) {
61
+ delete descriptor . get ;
62
+ delete descriptor . set ;
63
+ descriptor . value = serializeError ( descriptor . value ) ;
64
+ all [ key ] = descriptor ;
65
+ } else if ( 'value' in descriptor &&
66
+ typeof descriptor . value !== 'function' && typeof descriptor . value !== 'symbol' ) {
56
67
delete descriptor . get ;
57
68
delete descriptor . set ;
58
69
all [ key ] = descriptor ;
@@ -95,6 +106,10 @@ function inspect(...args) {
95
106
let serialize ;
96
107
function serializeError ( error ) {
97
108
if ( ! serialize ) serialize = require ( 'v8' ) . serialize ;
109
+ if ( typeof error === 'symbol' ) {
110
+ return Buffer . concat ( [ Buffer . from ( [ kInspectedSymbol ] ) ,
111
+ Buffer . from ( inspect ( error ) , 'utf8' ) ] ) ;
112
+ }
98
113
try {
99
114
if ( typeof error === 'object' &&
100
115
ObjectPrototypeToString ( error ) === '[object Error]' ) {
@@ -113,6 +128,15 @@ function serializeError(error) {
113
128
} catch {
114
129
// Continue regardless of error.
115
130
}
131
+ try {
132
+ if ( error != null &&
133
+ ObjectPrototypeHasOwnProperty ( error , customInspectSymbol ) ) {
134
+ return Buffer . concat ( [ Buffer . from ( [ kCustomInspecteObject ] ) ,
135
+ Buffer . from ( inspect ( error ) , 'utf8' ) ] ) ;
136
+ }
137
+ } catch {
138
+ // Continue regardless of error.
139
+ }
116
140
try {
117
141
const serialized = serialize ( error ) ;
118
142
return Buffer . concat ( [ Buffer . from ( [ kSerializedObject ] ) , serialized ] ) ;
@@ -123,6 +147,12 @@ function serializeError(error) {
123
147
Buffer . from ( inspect ( error ) , 'utf8' ) ] ) ;
124
148
}
125
149
150
+ function fromBuffer ( error ) {
151
+ return Buffer . from ( error . buffer ,
152
+ error . byteOffset + 1 ,
153
+ error . byteLength - 1 ) ;
154
+ }
155
+
126
156
let deserialize ;
127
157
function deserializeError ( error ) {
128
158
if ( ! deserialize ) deserialize = require ( 'v8' ) . deserialize ;
@@ -132,19 +162,27 @@ function deserializeError(error) {
132
162
const ctor = errors [ constructor ] ;
133
163
ObjectDefineProperty ( properties , SymbolToStringTag , {
134
164
__proto__ : null ,
135
- value : { value : 'Error' , configurable : true } ,
165
+ value : { __proto__ : null , value : 'Error' , configurable : true } ,
136
166
enumerable : true ,
137
167
} ) ;
168
+ if ( 'cause' in properties && 'value' in properties . cause ) {
169
+ properties . cause . value = deserializeError ( properties . cause . value ) ;
170
+ }
138
171
return ObjectCreate ( ctor . prototype , properties ) ;
139
172
}
140
173
case kSerializedObject :
141
174
return deserialize ( error . subarray ( 1 ) ) ;
142
- case kInspectedError : {
143
- const buf = Buffer . from ( error . buffer ,
144
- error . byteOffset + 1 ,
145
- error . byteLength - 1 ) ;
146
- return buf . toString ( 'utf8' ) ;
175
+ case kInspectedError :
176
+ return fromBuffer ( error ) . toString ( 'utf8' ) ;
177
+ case kInspectedSymbol : {
178
+ const buf = fromBuffer ( error ) ;
179
+ return SymbolFor ( buf . toString ( 'utf8' ) . substring ( 'Symbol(' . length , buf . length - 1 ) ) ;
147
180
}
181
+ case kCustomInspecteObject :
182
+ return {
183
+ __proto__ : null ,
184
+ [ customInspectSymbol ] : ( ) => fromBuffer ( error ) . toString ( 'utf8' ) ,
185
+ } ;
148
186
}
149
187
require ( 'assert' ) . fail ( 'This should not happen' ) ;
150
188
}
0 commit comments