@@ -175,9 +175,10 @@ const aggregateErrors = hideStackFrames((errors, message, code) => {
175
175
return err ;
176
176
} ) ;
177
177
178
+ const assert = require ( 'internal/assert' ) ;
179
+
178
180
// Lazily loaded
179
181
let util ;
180
- let assert ;
181
182
182
183
let internalUtil = null ;
183
184
function lazyInternalUtil ( ) {
@@ -371,42 +372,103 @@ function makeSystemErrorWithCode(key) {
371
372
}
372
373
373
374
function makeNodeErrorWithCode ( Base , key ) {
374
- return function NodeError ( ...args ) {
375
- const limit = Error . stackTraceLimit ;
376
- if ( isErrorStackTraceLimitWritable ( ) ) Error . stackTraceLimit = 0 ;
377
- const error = new Base ( ) ;
378
- // Reset the limit and setting the name property.
379
- if ( isErrorStackTraceLimitWritable ( ) ) Error . stackTraceLimit = limit ;
380
- const message = getMessage ( key , args , error ) ;
381
- ObjectDefineProperties ( error , {
382
- [ kIsNodeError ] : {
383
- __proto__ : null ,
384
- value : true ,
385
- enumerable : false ,
386
- writable : false ,
387
- configurable : true ,
388
- } ,
389
- message : {
390
- __proto__ : null ,
391
- value : message ,
392
- enumerable : false ,
393
- writable : true ,
394
- configurable : true ,
395
- } ,
396
- toString : {
397
- __proto__ : null ,
398
- value ( ) {
375
+ const msg = messages . get ( key ) ;
376
+ const expectedLength = typeof msg !== 'string' ? - 1 : getExpectedArgumentLength ( msg ) ;
377
+
378
+ switch ( expectedLength ) {
379
+ case 0 : {
380
+ class NodeError extends Base {
381
+ code = key ;
382
+
383
+ constructor ( ...args ) {
384
+ assert (
385
+ args . length === 0 ,
386
+ `Code: ${ key } ; The provided arguments length (${ args . length } ) does not ` +
387
+ `match the required ones (${ expectedLength } ).` ,
388
+ ) ;
389
+ super ( msg ) ;
390
+ }
391
+
392
+ // This is a workaround for wpt tests that expect that the error
393
+ // constructor has a `name` property of the base class.
394
+ get [ 'constructor' ] ( ) {
395
+ return Base ;
396
+ }
397
+
398
+ get [ kIsNodeError ] ( ) {
399
+ return true ;
400
+ }
401
+
402
+ toString ( ) {
399
403
return `${ this . name } [${ key } ]: ${ this . message } ` ;
400
- } ,
401
- enumerable : false ,
402
- writable : true ,
403
- configurable : true ,
404
- } ,
405
- } ) ;
406
- captureLargerStackTrace ( error ) ;
407
- error . code = key ;
408
- return error ;
409
- } ;
404
+ }
405
+ }
406
+ return NodeError ;
407
+ }
408
+ case - 1 : {
409
+ class NodeError extends Base {
410
+ code = key ;
411
+
412
+ constructor ( ...args ) {
413
+ super ( ) ;
414
+ ObjectDefineProperty ( this , 'message' , {
415
+ __proto__ : null ,
416
+ value : getMessage ( key , args , this ) ,
417
+ enumerable : false ,
418
+ writable : true ,
419
+ configurable : true ,
420
+ } ) ;
421
+ }
422
+
423
+ // This is a workaround for wpt tests that expect that the error
424
+ // constructor has a `name` property of the base class.
425
+ get [ 'constructor' ] ( ) {
426
+ return Base ;
427
+ }
428
+
429
+ get [ kIsNodeError ] ( ) {
430
+ return true ;
431
+ }
432
+
433
+ toString ( ) {
434
+ return `${ this . name } [${ key } ]: ${ this . message } ` ;
435
+ }
436
+ }
437
+ return NodeError ;
438
+ }
439
+ default : {
440
+
441
+ class NodeError extends Base {
442
+ code = key ;
443
+
444
+ constructor ( ...args ) {
445
+ assert (
446
+ args . length === expectedLength ,
447
+ `Code: ${ key } ; The provided arguments length (${ args . length } ) does not ` +
448
+ `match the required ones (${ expectedLength } ).` ,
449
+ ) ;
450
+
451
+ ArrayPrototypeUnshift ( args , msg ) ;
452
+ super ( ReflectApply ( lazyInternalUtilInspect ( ) . format , null , args ) ) ;
453
+ }
454
+
455
+ // This is a workaround for wpt tests that expect that the error
456
+ // constructor has a `name` property of the base class.
457
+ get [ 'constructor' ] ( ) {
458
+ return Base ;
459
+ }
460
+
461
+ get [ kIsNodeError ] ( ) {
462
+ return true ;
463
+ }
464
+
465
+ toString ( ) {
466
+ return `${ this . name } [${ key } ]: ${ this . message } ` ;
467
+ }
468
+ }
469
+ return NodeError ;
470
+ }
471
+ }
410
472
}
411
473
412
474
/**
@@ -443,11 +505,16 @@ function E(sym, val, def, ...otherClasses) {
443
505
codes [ sym ] = def ;
444
506
}
445
507
508
+ function getExpectedArgumentLength ( msg ) {
509
+ let expectedLength = 0 ;
510
+ const regex = / % [ d f i j o O s ] / g;
511
+ while ( RegExpPrototypeExec ( regex , msg ) !== null ) expectedLength ++ ;
512
+ return expectedLength ;
513
+ }
514
+
446
515
function getMessage ( key , args , self ) {
447
516
const msg = messages . get ( key ) ;
448
517
449
- assert ??= require ( 'internal/assert' ) ;
450
-
451
518
if ( typeof msg === 'function' ) {
452
519
assert (
453
520
msg . length <= args . length , // Default options do not count.
@@ -457,9 +524,7 @@ function getMessage(key, args, self) {
457
524
return ReflectApply ( msg , self , args ) ;
458
525
}
459
526
460
- const regex = / % [ d f i j o O s ] / g;
461
- let expectedLength = 0 ;
462
- while ( RegExpPrototypeExec ( regex , msg ) !== null ) expectedLength ++ ;
527
+ const expectedLength = getExpectedArgumentLength ( msg ) ;
463
528
assert (
464
529
expectedLength === args . length ,
465
530
`Code: ${ key } ; The provided arguments length (${ args . length } ) does not ` +
@@ -1476,8 +1541,7 @@ E('ERR_NETWORK_IMPORT_DISALLOWED',
1476
1541
"import of '%s' by %s is not supported: %s" , Error ) ;
1477
1542
E ( 'ERR_NOT_BUILDING_SNAPSHOT' ,
1478
1543
'Operation cannot be invoked when not building startup snapshot' , Error ) ;
1479
- E ( 'ERR_NOT_SUPPORTED_IN_SNAPSHOT' ,
1480
- '%s is not supported in startup snapshot' , Error ) ;
1544
+ E ( 'ERR_NOT_SUPPORTED_IN_SNAPSHOT' , '%s is not supported in startup snapshot' , Error ) ;
1481
1545
E ( 'ERR_NO_CRYPTO' ,
1482
1546
'Node.js is not compiled with OpenSSL crypto support' , Error ) ;
1483
1547
E ( 'ERR_NO_ICU' ,
0 commit comments