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
@@ -40,6 +43,7 @@ const kCircularReference = 5;
4043
4144const kSymbolStringLength = 'Symbol(' . length ;
4245
46+ // TODO: implement specific logic for AggregateError/SuppressedError
4347const errors = {
4448 Error, TypeError, RangeError, URIError, SyntaxError, ReferenceError, EvalError,
4549} ;
@@ -166,16 +170,17 @@ function deserializeError(error) {
166170 switch ( error [ 0 ] ) {
167171 case kSerializedError : {
168172 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- } ) ;
173+ assert ( errorConstructorNames . has ( constructor ) , 'Invalid constructor' ) ;
175174 if ( 'cause' in properties && 'value' in properties . cause ) {
176175 properties . cause . value = deserializeError ( properties . cause . value ) ;
177176 }
178- return ObjectCreate ( ctor . prototype , properties ) ;
177+ // Invoke the Error constructor to gain an object with an [[ErrorData]] internal slot
178+ const ret = ReflectConstruct ( Error , [ ] , errors [ constructor ] ) ;
179+ // Delete any properties defined by the Error constructor before assigning from source
180+ ArrayPrototypeForEach ( ObjectGetOwnPropertyNames ( ret ) , ( key ) => {
181+ ReflectDeleteProperty ( ret , key ) ;
182+ } ) ;
183+ return ObjectDefineProperties ( ret , properties ) ;
179184 }
180185 case kSerializedObject :
181186 return deserialize ( error . subarray ( 1 ) ) ;
@@ -196,7 +201,7 @@ function deserializeError(error) {
196201 [ customInspectSymbol ] : ( ) => '[Circular object]' ,
197202 } ;
198203 }
199- require ( ' assert' ) . fail ( 'This should not happen ' ) ;
204+ assert . fail ( 'Unknown serializer flag ' ) ;
200205}
201206
202207module . exports = { serializeError, deserializeError } ;
0 commit comments