66 EvalError,
77 FunctionPrototypeCall,
88 ObjectAssign,
9- ObjectCreate ,
9+ ObjectDefineProperties ,
1010 ObjectDefineProperty,
1111 ObjectGetOwnPropertyDescriptor,
1212 ObjectGetOwnPropertyNames,
@@ -15,11 +15,12 @@ const {
1515 ObjectPrototypeToString,
1616 RangeError,
1717 ReferenceError,
18+ ReflectConstruct,
19+ ReflectDeleteProperty,
1820 SafeSet,
1921 StringFromCharCode,
2022 StringPrototypeSubstring,
2123 SymbolFor,
22- SymbolToStringTag,
2324 SyntaxError,
2425 TypeError,
2526 TypedArrayPrototypeGetBuffer,
@@ -28,6 +29,8 @@ const {
2829 URIError,
2930} = primordials ;
3031
32+ const assert = require ( 'internal/assert' ) ;
33+
3134const { Buffer } = require ( 'buffer' ) ;
3235const { inspect : { custom : customInspectSymbol } } = require ( 'util' ) ;
3336
@@ -166,16 +169,17 @@ function deserializeError(error) {
166169 switch ( error [ 0 ] ) {
167170 case kSerializedError : {
168171 const { constructor, properties } = deserialize ( error . subarray ( 1 ) ) ;
169- const ctor = errors [ constructor ] ;
170- ObjectDefineProperty ( properties , SymbolToStringTag , {
171- __proto__ : null ,
172- value : { __proto__ : null , value : 'Error' , configurable : true } ,
173- enumerable : true ,
174- } ) ;
172+ assert ( errorConstructorNames . has ( constructor ) , 'Invalid constructor' ) ;
175173 if ( 'cause' in properties && 'value' in properties . cause ) {
176174 properties . cause . value = deserializeError ( properties . cause . value ) ;
177175 }
178- return ObjectCreate ( ctor . prototype , properties ) ;
176+ // Invoke the Error constructor to gain an object with an [[ErrorData]] internal slot
177+ const ret = ReflectConstruct ( Error , [ ] , errors [ constructor ] ) ;
178+ // Delete any properties defined by the Error constructor before assigning from source
179+ ArrayPrototypeForEach ( ObjectGetOwnPropertyNames ( ret ) , ( key ) => {
180+ ReflectDeleteProperty ( ret , key ) ;
181+ } ) ;
182+ return ObjectDefineProperties ( ret , properties ) ;
179183 }
180184 case kSerializedObject :
181185 return deserialize ( error . subarray ( 1 ) ) ;
@@ -196,7 +200,7 @@ function deserializeError(error) {
196200 [ customInspectSymbol ] : ( ) => '[Circular object]' ,
197201 } ;
198202 }
199- require ( ' assert' ) . fail ( 'This should not happen ' ) ;
203+ assert . fail ( 'Unknown serializer flag ' ) ;
200204}
201205
202206module . exports = { serializeError, deserializeError } ;
0 commit comments