@@ -33,6 +33,7 @@ const {
33
33
Number,
34
34
NumberIsInteger,
35
35
ObjectDefineProperty,
36
+ ObjectDefineProperties,
36
37
ObjectIsExtensible,
37
38
ObjectGetOwnPropertyDescriptor,
38
39
ObjectKeys,
@@ -58,6 +59,8 @@ const {
58
59
URIError,
59
60
} = primordials ;
60
61
62
+ const kIsNodeError = Symbol ( 'kIsNodeError' ) ;
63
+
61
64
const isWindows = process . platform === 'win32' ;
62
65
63
66
const messages = new SafeMap ( ) ;
@@ -116,7 +119,12 @@ const prepareStackTrace = (globalThis, error, trace) => {
116
119
// Error: Message
117
120
// at function (file)
118
121
// 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
+ }
120
128
if ( trace . length === 0 ) {
121
129
return errorString ;
122
130
}
@@ -186,27 +194,6 @@ function lazyBuffer() {
186
194
return buffer ;
187
195
}
188
196
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
-
210
197
function isErrorStackTraceLimitWritable ( ) {
211
198
const desc = ObjectGetOwnPropertyDescriptor ( Error , 'stackTraceLimit' ) ;
212
199
if ( desc === undefined ) {
@@ -242,43 +229,55 @@ class SystemError extends Error {
242
229
if ( context . dest !== undefined )
243
230
message += ` => ${ context . dest } ` ;
244
231
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 ) ;
252
233
253
234
this . code = key ;
254
235
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 ,
265
242
} ,
266
- set : ( value ) => {
267
- context . errno = value ;
243
+ name : {
244
+ value : 'SystemError' ,
245
+ enumerable : false ,
246
+ writable : true ,
247
+ configurable : true ,
268
248
} ,
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 ,
276
260
} ,
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 ,
279
280
} ,
280
- enumerable : true ,
281
- configurable : true
282
281
} ) ;
283
282
284
283
if ( context . path !== undefined ) {
@@ -346,21 +345,29 @@ function makeNodeErrorWithCode(Base, key) {
346
345
// Reset the limit and setting the name property.
347
346
if ( isErrorStackTraceLimitWritable ( ) ) Error . stackTraceLimit = limit ;
348
347
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 ,
358
368
} ,
359
- enumerable : false ,
360
- writable : true ,
361
- configurable : true ,
362
369
} ) ;
363
- addCodeToName ( error , Base . name , key ) ;
370
+ captureLargerStackTrace ( error ) ;
364
371
error . code = key ;
365
372
return error ;
366
373
} ;
@@ -792,7 +799,6 @@ class AbortError extends Error {
792
799
}
793
800
}
794
801
module . exports = {
795
- addCodeToName, // Exported for NghttpError
796
802
aggregateTwoErrors,
797
803
codes,
798
804
dnsException,
@@ -815,7 +821,9 @@ module.exports = {
815
821
maybeOverridePrepareStackTrace,
816
822
overrideStackTrace,
817
823
kEnhanceStackBeforeInspector,
818
- fatalExceptionStackEnhancers
824
+ fatalExceptionStackEnhancers,
825
+ kIsNodeError,
826
+ captureLargerStackTrace,
819
827
} ;
820
828
821
829
// To declare an error message, use the E(sym, val, def) function above. The sym
0 commit comments