@@ -410,6 +410,26 @@ function isStackOverflowError(err) {
410
410
err . message === maxStack_ErrorMessage ;
411
411
}
412
412
413
+ function oneOf ( expected , thing ) {
414
+ assert ( typeof thing === 'string' , '`thing` has to be of type string' ) ;
415
+ if ( Array . isArray ( expected ) ) {
416
+ const len = expected . length ;
417
+ assert ( len > 0 ,
418
+ 'At least one expected value needs to be specified' ) ;
419
+ expected = expected . map ( ( i ) => String ( i ) ) ;
420
+ if ( len > 2 ) {
421
+ return `one of ${ thing } ${ expected . slice ( 0 , len - 1 ) . join ( ', ' ) } , or ` +
422
+ expected [ len - 1 ] ;
423
+ } else if ( len === 2 ) {
424
+ return `one of ${ thing } ${ expected [ 0 ] } or ${ expected [ 1 ] } ` ;
425
+ } else {
426
+ return `of ${ thing } ${ expected [ 0 ] } ` ;
427
+ }
428
+ } else {
429
+ return `of ${ thing } ${ String ( expected ) } ` ;
430
+ }
431
+ }
432
+
413
433
module . exports = {
414
434
dnsException,
415
435
errnoException,
@@ -444,7 +464,15 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError);
444
464
E ( 'ERR_ASSERTION' , '%s' , Error ) ;
445
465
E ( 'ERR_ASYNC_CALLBACK' , '%s must be a function' , TypeError ) ;
446
466
E ( 'ERR_ASYNC_TYPE' , 'Invalid name for async "type": %s' , TypeError ) ;
447
- E ( 'ERR_BUFFER_OUT_OF_BOUNDS' , bufferOutOfBounds , RangeError ) ;
467
+ E ( 'ERR_BUFFER_OUT_OF_BOUNDS' ,
468
+ // Using a default argument here is important so the argument is not counted
469
+ // towards `Function#length`.
470
+ ( name = undefined ) => {
471
+ if ( name ) {
472
+ return `"${ name } " is outside of buffer bounds` ;
473
+ }
474
+ return 'Attempt to write outside buffer bounds' ;
475
+ } , RangeError ) ;
448
476
E ( 'ERR_BUFFER_TOO_LARGE' ,
449
477
`Cannot create a Buffer larger than 0x${ kMaxLength . toString ( 16 ) } bytes` ,
450
478
RangeError ) ;
@@ -582,7 +610,32 @@ E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
582
610
E ( 'ERR_INSPECTOR_NOT_AVAILABLE' , 'Inspector is not available' , Error ) ;
583
611
E ( 'ERR_INSPECTOR_NOT_CONNECTED' , 'Session is not connected' , Error ) ;
584
612
E ( 'ERR_INVALID_ADDRESS_FAMILY' , 'Invalid address family: %s' , RangeError ) ;
585
- E ( 'ERR_INVALID_ARG_TYPE' , invalidArgType , TypeError ) ;
613
+ E ( 'ERR_INVALID_ARG_TYPE' ,
614
+ ( name , expected , actual ) => {
615
+ assert ( typeof name === 'string' , "'name' must be a string" ) ;
616
+
617
+ // determiner: 'must be' or 'must not be'
618
+ let determiner ;
619
+ if ( typeof expected === 'string' && expected . startsWith ( 'not ' ) ) {
620
+ determiner = 'must not be' ;
621
+ expected = expected . replace ( / ^ n o t / , '' ) ;
622
+ } else {
623
+ determiner = 'must be' ;
624
+ }
625
+
626
+ let msg ;
627
+ if ( name . endsWith ( ' argument' ) ) {
628
+ // For cases like 'first argument'
629
+ msg = `The ${ name } ${ determiner } ${ oneOf ( expected , 'type' ) } ` ;
630
+ } else {
631
+ const type = name . includes ( '.' ) ? 'property' : 'argument' ;
632
+ msg = `The "${ name } " ${ type } ${ determiner } ${ oneOf ( expected , 'type' ) } ` ;
633
+ }
634
+
635
+ // TODO(BridgeAR): Improve the output by showing `null` and similar.
636
+ msg += `. Received type ${ typeof actual } ` ;
637
+ return msg ;
638
+ } , TypeError ) ;
586
639
E ( 'ERR_INVALID_ARG_VALUE' , ( name , value , reason = 'is invalid' ) => {
587
640
let inspected = util . inspect ( value ) ;
588
641
if ( inspected . length > 128 ) {
@@ -598,7 +651,16 @@ E('ERR_INVALID_ASYNC_ID', 'Invalid %s value: %s', RangeError);
598
651
E ( 'ERR_INVALID_BUFFER_SIZE' ,
599
652
'Buffer size must be a multiple of %s' , RangeError ) ;
600
653
E ( 'ERR_INVALID_CALLBACK' , 'Callback must be a function' , TypeError ) ;
601
- E ( 'ERR_INVALID_CHAR' , invalidChar , TypeError ) ;
654
+ E ( 'ERR_INVALID_CHAR' ,
655
+ // Using a default argument here is important so the argument is not counted
656
+ // towards `Function#length`.
657
+ ( name , field = undefined ) => {
658
+ let msg = `Invalid character in ${ name } ` ;
659
+ if ( field !== undefined ) {
660
+ msg += ` ["${ field } "]` ;
661
+ }
662
+ return msg ;
663
+ } , TypeError ) ;
602
664
E ( 'ERR_INVALID_CURSOR_POS' ,
603
665
'Cannot set cursor row without setting its column' , TypeError ) ;
604
666
E ( 'ERR_INVALID_DOMAIN_NAME' , 'Unable to determine the domain name' , TypeError ) ;
@@ -648,7 +710,26 @@ E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected', Error);
648
710
E ( 'ERR_IPC_ONE_PIPE' , 'Child process can have only one IPC pipe' , Error ) ;
649
711
E ( 'ERR_IPC_SYNC_FORK' , 'IPC cannot be used with synchronous forks' , Error ) ;
650
712
E ( 'ERR_METHOD_NOT_IMPLEMENTED' , 'The %s method is not implemented' , Error ) ;
651
- E ( 'ERR_MISSING_ARGS' , missingArgs , TypeError ) ;
713
+ E ( 'ERR_MISSING_ARGS' ,
714
+ ( ...args ) => {
715
+ assert ( args . length > 0 , 'At least one arg needs to be specified' ) ;
716
+ let msg = 'The ' ;
717
+ const len = args . length ;
718
+ args = args . map ( ( a ) => `"${ a } "` ) ;
719
+ switch ( len ) {
720
+ case 1 :
721
+ msg += `${ args [ 0 ] } argument` ;
722
+ break ;
723
+ case 2 :
724
+ msg += `${ args [ 0 ] } and ${ args [ 1 ] } arguments` ;
725
+ break ;
726
+ default :
727
+ msg += args . slice ( 0 , len - 1 ) . join ( ', ' ) ;
728
+ msg += `, and ${ args [ len - 1 ] } arguments` ;
729
+ break ;
730
+ }
731
+ return `${ msg } must be specified` ;
732
+ } , TypeError ) ;
652
733
E ( 'ERR_MISSING_MODULE' , 'Cannot find module %s' , Error ) ;
653
734
E ( 'ERR_MODULE_RESOLUTION_LEGACY' ,
654
735
'%s not found by import in %s.' +
@@ -669,7 +750,13 @@ E('ERR_NO_CRYPTO',
669
750
E ( 'ERR_NO_ICU' ,
670
751
'%s is not supported on Node.js compiled without ICU' , TypeError ) ;
671
752
E ( 'ERR_NO_LONGER_SUPPORTED' , '%s is no longer supported' , Error ) ;
672
- E ( 'ERR_OUT_OF_RANGE' , outOfRange , RangeError ) ;
753
+ E ( 'ERR_OUT_OF_RANGE' ,
754
+ ( name , range , value ) => {
755
+ let msg = `The value of "${ name } " is out of range.` ;
756
+ if ( range !== undefined ) msg += ` It must be ${ range } .` ;
757
+ msg += ` Received ${ value } ` ;
758
+ return msg ;
759
+ } , RangeError ) ;
673
760
E ( 'ERR_REQUIRE_ESM' , 'Must use import to load ES Module: %s' , Error ) ;
674
761
E ( 'ERR_SCRIPT_EXECUTION_INTERRUPTED' ,
675
762
'Script execution was interrupted by `SIGINT`.' , Error ) ;
@@ -765,95 +852,3 @@ E('ERR_VM_MODULE_NOT_MODULE',
765
852
'Provided module is not an instance of Module' , Error ) ;
766
853
E ( 'ERR_VM_MODULE_STATUS' , 'Module status %s' , Error ) ;
767
854
E ( 'ERR_ZLIB_INITIALIZATION_FAILED' , 'Initialization failed' , Error ) ;
768
-
769
- function invalidArgType ( name , expected , actual ) {
770
- assert ( typeof name === 'string' , "'name' must be a string" ) ;
771
-
772
- // determiner: 'must be' or 'must not be'
773
- let determiner ;
774
- if ( typeof expected === 'string' && expected . startsWith ( 'not ' ) ) {
775
- determiner = 'must not be' ;
776
- expected = expected . replace ( / ^ n o t / , '' ) ;
777
- } else {
778
- determiner = 'must be' ;
779
- }
780
-
781
- let msg ;
782
- if ( name . endsWith ( ' argument' ) ) {
783
- // For cases like 'first argument'
784
- msg = `The ${ name } ${ determiner } ${ oneOf ( expected , 'type' ) } ` ;
785
- } else {
786
- const type = name . includes ( '.' ) ? 'property' : 'argument' ;
787
- msg = `The "${ name } " ${ type } ${ determiner } ${ oneOf ( expected , 'type' ) } ` ;
788
- }
789
-
790
- // TODO(BridgeAR): Improve the output by showing `null` and similar.
791
- msg += `. Received type ${ typeof actual } ` ;
792
- return msg ;
793
- }
794
-
795
- function missingArgs ( ...args ) {
796
- assert ( args . length > 0 , 'At least one arg needs to be specified' ) ;
797
- let msg = 'The ' ;
798
- const len = args . length ;
799
- args = args . map ( ( a ) => `"${ a } "` ) ;
800
- switch ( len ) {
801
- case 1 :
802
- msg += `${ args [ 0 ] } argument` ;
803
- break ;
804
- case 2 :
805
- msg += `${ args [ 0 ] } and ${ args [ 1 ] } arguments` ;
806
- break ;
807
- default :
808
- msg += args . slice ( 0 , len - 1 ) . join ( ', ' ) ;
809
- msg += `, and ${ args [ len - 1 ] } arguments` ;
810
- break ;
811
- }
812
- return `${ msg } must be specified` ;
813
- }
814
-
815
- function oneOf ( expected , thing ) {
816
- assert ( typeof thing === 'string' , '`thing` has to be of type string' ) ;
817
- if ( Array . isArray ( expected ) ) {
818
- const len = expected . length ;
819
- assert ( len > 0 ,
820
- 'At least one expected value needs to be specified' ) ;
821
- expected = expected . map ( ( i ) => String ( i ) ) ;
822
- if ( len > 2 ) {
823
- return `one of ${ thing } ${ expected . slice ( 0 , len - 1 ) . join ( ', ' ) } , or ` +
824
- expected [ len - 1 ] ;
825
- } else if ( len === 2 ) {
826
- return `one of ${ thing } ${ expected [ 0 ] } or ${ expected [ 1 ] } ` ;
827
- } else {
828
- return `of ${ thing } ${ expected [ 0 ] } ` ;
829
- }
830
- } else {
831
- return `of ${ thing } ${ String ( expected ) } ` ;
832
- }
833
- }
834
-
835
- // Using a default argument here is important so the argument is not counted
836
- // towards `Function#length`.
837
- function bufferOutOfBounds ( name = undefined ) {
838
- if ( name ) {
839
- return `"${ name } " is outside of buffer bounds` ;
840
- }
841
- return 'Attempt to write outside buffer bounds' ;
842
- }
843
-
844
- // Using a default argument here is important so the argument is not counted
845
- // towards `Function#length`.
846
- function invalidChar ( name , field = undefined ) {
847
- let msg = `Invalid character in ${ name } ` ;
848
- if ( field !== undefined ) {
849
- msg += ` ["${ field } "]` ;
850
- }
851
- return msg ;
852
- }
853
-
854
- function outOfRange ( name , range , value ) {
855
- let msg = `The value of "${ name } " is out of range.` ;
856
- if ( range !== undefined ) msg += ` It must be ${ range } .` ;
857
- msg += ` Received ${ value } ` ;
858
- return msg ;
859
- }
0 commit comments