@@ -13,19 +13,27 @@ const {
1313 ObjectGetOwnPropertyNames,
1414 ObjectGetPrototypeOf,
1515 ObjectKeys,
16+ ObjectPrototypeHasOwnProperty,
1617 ObjectPrototypeToString,
1718 RangeError,
1819 ReferenceError,
1920 SafeSet,
21+ StringPrototypeSubstring,
2022 SymbolToStringTag,
2123 SyntaxError,
24+ SymbolFor,
2225 TypeError,
2326 URIError,
2427} = primordials ;
28+ const { inspect : { custom : customInspectSymbol } } = require ( 'util' ) ;
2529
2630const kSerializedError = 0 ;
2731const kSerializedObject = 1 ;
2832const kInspectedError = 2 ;
33+ const kInspectedSymbol = 3 ;
34+ const kCustomInspectedObject = 4 ;
35+
36+ const kSymbolStringLength = 'Symbol(' . length ;
2937
3038const errors = {
3139 Error, TypeError, RangeError, URIError, SyntaxError, ReferenceError, EvalError,
@@ -52,7 +60,13 @@ function TryGetAllProperties(object, target = object) {
5260 // Continue regardless of error.
5361 }
5462 }
55- if ( 'value' in descriptor && typeof descriptor . value !== 'function' ) {
63+ if ( key === 'cause' ) {
64+ delete descriptor . get ;
65+ delete descriptor . set ;
66+ descriptor . value = serializeError ( descriptor . value ) ;
67+ all [ key ] = descriptor ;
68+ } else if ( 'value' in descriptor &&
69+ typeof descriptor . value !== 'function' && typeof descriptor . value !== 'symbol' ) {
5670 delete descriptor . get ;
5771 delete descriptor . set ;
5872 all [ key ] = descriptor ;
@@ -95,6 +109,10 @@ function inspect(...args) {
95109let serialize ;
96110function serializeError ( error ) {
97111 if ( ! serialize ) serialize = require ( 'v8' ) . serialize ;
112+ if ( typeof error === 'symbol' ) {
113+ return Buffer . concat ( [ Buffer . from ( [ kInspectedSymbol ] ) ,
114+ Buffer . from ( inspect ( error ) , 'utf8' ) ] ) ;
115+ }
98116 try {
99117 if ( typeof error === 'object' &&
100118 ObjectPrototypeToString ( error ) === '[object Error]' ) {
@@ -113,6 +131,15 @@ function serializeError(error) {
113131 } catch {
114132 // Continue regardless of error.
115133 }
134+ try {
135+ if ( error != null &&
136+ ObjectPrototypeHasOwnProperty ( error , customInspectSymbol ) ) {
137+ return Buffer . concat ( [ Buffer . from ( [ kCustomInspectedObject ] ) ,
138+ Buffer . from ( inspect ( error ) , 'utf8' ) ] ) ;
139+ }
140+ } catch {
141+ // Continue regardless of error.
142+ }
116143 try {
117144 const serialized = serialize ( error ) ;
118145 return Buffer . concat ( [ Buffer . from ( [ kSerializedObject ] ) , serialized ] ) ;
@@ -123,6 +150,12 @@ function serializeError(error) {
123150 Buffer . from ( inspect ( error ) , 'utf8' ) ] ) ;
124151}
125152
153+ function fromBuffer ( error ) {
154+ return Buffer . from ( error . buffer ,
155+ error . byteOffset + 1 ,
156+ error . byteLength - 1 ) ;
157+ }
158+
126159let deserialize ;
127160function deserializeError ( error ) {
128161 if ( ! deserialize ) deserialize = require ( 'v8' ) . deserialize ;
@@ -132,19 +165,27 @@ function deserializeError(error) {
132165 const ctor = errors [ constructor ] ;
133166 ObjectDefineProperty ( properties , SymbolToStringTag , {
134167 __proto__ : null ,
135- value : { value : 'Error' , configurable : true } ,
168+ value : { __proto__ : null , value : 'Error' , configurable : true } ,
136169 enumerable : true ,
137170 } ) ;
171+ if ( 'cause' in properties && 'value' in properties . cause ) {
172+ properties . cause . value = deserializeError ( properties . cause . value ) ;
173+ }
138174 return ObjectCreate ( ctor . prototype , properties ) ;
139175 }
140176 case kSerializedObject :
141177 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' ) ;
178+ case kInspectedError :
179+ return fromBuffer ( error ) . toString ( 'utf8' ) ;
180+ case kInspectedSymbol : {
181+ const buf = fromBuffer ( error ) ;
182+ return SymbolFor ( StringPrototypeSubstring ( buf . toString ( 'utf8' ) , kSymbolStringLength , buf . length - 1 ) ) ;
147183 }
184+ case kCustomInspectedObject :
185+ return {
186+ __proto__ : null ,
187+ [ customInspectSymbol ] : ( ) => fromBuffer ( error ) . toString ( 'utf8' ) ,
188+ } ;
148189 }
149190 require ( 'assert' ) . fail ( 'This should not happen' ) ;
150191}
0 commit comments