@@ -33,6 +33,7 @@ const {
3333 Number,
3434 NumberIsInteger,
3535 ObjectDefineProperty,
36+ ObjectDefineProperties,
3637 ObjectIsExtensible,
3738 ObjectGetOwnPropertyDescriptor,
3839 ObjectKeys,
@@ -58,6 +59,8 @@ const {
5859 URIError,
5960} = primordials ;
6061
62+ const kIsNodeError = Symbol ( 'kIsNodeError' ) ;
63+
6164const isWindows = process . platform === 'win32' ;
6265
6366const messages = new SafeMap ( ) ;
@@ -116,7 +119,12 @@ const prepareStackTrace = (globalThis, error, trace) => {
116119 // Error: Message
117120 // at function (file)
118121 // at file
119- const errorString = ErrorPrototypeToString ( error ) ;
122+ let errorString ;
123+ if ( kIsNodeError in error ) {
124+ errorString = `${ error . name } [${ error . code } ]: ${ error . message } ` ;
125+ } else {
126+ errorString = ErrorPrototypeToString ( error ) ;
127+ }
120128 if ( trace . length === 0 ) {
121129 return errorString ;
122130 }
@@ -186,27 +194,6 @@ function lazyBuffer() {
186194 return buffer ;
187195}
188196
189- const addCodeToName = hideStackFrames ( function addCodeToName ( err , name , code ) {
190- // Set the stack
191- err = captureLargerStackTrace ( err ) ;
192- // Add the error code to the name to include it in the stack trace.
193- err . name = `${ name } [${ code } ]` ;
194- // Access the stack to generate the error message including the error code
195- // from the name.
196- err . stack ; // eslint-disable-line no-unused-expressions
197- // Reset the name to the actual name.
198- if ( name === 'SystemError' ) {
199- ObjectDefineProperty ( err , 'name' , {
200- value : name ,
201- enumerable : false ,
202- writable : true ,
203- configurable : true
204- } ) ;
205- } else {
206- delete err . name ;
207- }
208- } ) ;
209-
210197function isErrorStackTraceLimitWritable ( ) {
211198 const desc = ObjectGetOwnPropertyDescriptor ( Error , 'stackTraceLimit' ) ;
212199 if ( desc === undefined ) {
@@ -242,43 +229,55 @@ class SystemError extends Error {
242229 if ( context . dest !== undefined )
243230 message += ` => ${ context . dest } ` ;
244231
245- ObjectDefineProperty ( this , 'message' , {
246- value : message ,
247- enumerable : false ,
248- writable : true ,
249- configurable : true
250- } ) ;
251- addCodeToName ( this , 'SystemError' , key ) ;
232+ captureLargerStackTrace ( this ) ;
252233
253234 this . code = key ;
254235
255- ObjectDefineProperty ( this , 'info' , {
256- value : context ,
257- enumerable : true ,
258- configurable : true ,
259- writable : false
260- } ) ;
261-
262- ObjectDefineProperty ( this , 'errno' , {
263- get ( ) {
264- return context . errno ;
236+ ObjectDefineProperties ( this , {
237+ [ kIsNodeError ] : {
238+ value : true ,
239+ enumerable : false ,
240+ writable : false ,
241+ configurable : true ,
265242 } ,
266- set : ( value ) => {
267- context . errno = value ;
243+ name : {
244+ value : 'SystemError' ,
245+ enumerable : false ,
246+ writable : true ,
247+ configurable : true ,
268248 } ,
269- enumerable : true ,
270- configurable : true
271- } ) ;
272-
273- ObjectDefineProperty ( this , 'syscall' , {
274- get ( ) {
275- return context . syscall ;
249+ message : {
250+ value : message ,
251+ enumerable : false ,
252+ writable : true ,
253+ configurable : true ,
254+ } ,
255+ info : {
256+ value : context ,
257+ enumerable : true ,
258+ configurable : true ,
259+ writable : false ,
276260 } ,
277- set : ( value ) => {
278- context . syscall = value ;
261+ errno : {
262+ get ( ) {
263+ return context . errno ;
264+ } ,
265+ set : ( value ) => {
266+ context . errno = value ;
267+ } ,
268+ enumerable : true ,
269+ configurable : true ,
270+ } ,
271+ syscall : {
272+ get ( ) {
273+ return context . syscall ;
274+ } ,
275+ set : ( value ) => {
276+ context . syscall = value ;
277+ } ,
278+ enumerable : true ,
279+ configurable : true ,
279280 } ,
280- enumerable : true ,
281- configurable : true
282281 } ) ;
283282
284283 if ( context . path !== undefined ) {
@@ -346,21 +345,29 @@ function makeNodeErrorWithCode(Base, key) {
346345 // Reset the limit and setting the name property.
347346 if ( isErrorStackTraceLimitWritable ( ) ) Error . stackTraceLimit = limit ;
348347 const message = getMessage ( key , args , error ) ;
349- ObjectDefineProperty ( error , 'message' , {
350- value : message ,
351- enumerable : false ,
352- writable : true ,
353- configurable : true ,
354- } ) ;
355- ObjectDefineProperty ( error , 'toString' , {
356- value ( ) {
357- return `${ this . name } [${ key } ]: ${ this . message } ` ;
348+ ObjectDefineProperties ( error , {
349+ [ kIsNodeError ] : {
350+ value : true ,
351+ enumerable : false ,
352+ writable : false ,
353+ configurable : true ,
354+ } ,
355+ message : {
356+ value : message ,
357+ enumerable : false ,
358+ writable : true ,
359+ configurable : true ,
360+ } ,
361+ toString : {
362+ value ( ) {
363+ return `${ this . name } [${ key } ]: ${ this . message } ` ;
364+ } ,
365+ enumerable : false ,
366+ writable : true ,
367+ configurable : true ,
358368 } ,
359- enumerable : false ,
360- writable : true ,
361- configurable : true ,
362369 } ) ;
363- addCodeToName ( error , Base . name , key ) ;
370+ captureLargerStackTrace ( error ) ;
364371 error . code = key ;
365372 return error ;
366373 } ;
@@ -792,7 +799,6 @@ class AbortError extends Error {
792799 }
793800}
794801module . exports = {
795- addCodeToName, // Exported for NghttpError
796802 aggregateTwoErrors,
797803 codes,
798804 dnsException,
@@ -815,7 +821,9 @@ module.exports = {
815821 maybeOverridePrepareStackTrace,
816822 overrideStackTrace,
817823 kEnhanceStackBeforeInspector,
818- fatalExceptionStackEnhancers
824+ fatalExceptionStackEnhancers,
825+ kIsNodeError,
826+ captureLargerStackTrace,
819827} ;
820828
821829// To declare an error message, use the E(sym, val, def) function above. The sym
0 commit comments