@@ -416,20 +416,30 @@ function internalAssert(condition, message) {
416
416
}
417
417
}
418
418
419
- function message ( key , args ) {
419
+ function message ( key , args = [ ] ) {
420
420
const msg = messages . get ( key ) ;
421
- internalAssert ( msg , `An invalid error message key was used: ${ key } .` ) ;
422
- let fmt ;
423
421
if ( util === undefined ) util = require ( 'util' ) ;
422
+
424
423
if ( typeof msg === 'function' ) {
425
- fmt = msg ;
426
- } else {
427
- fmt = util . format ;
428
- if ( args === undefined || args . length === 0 )
429
- return msg ;
430
- args . unshift ( msg ) ;
424
+ internalAssert (
425
+ msg . length <= args . length , // Default options do not count.
426
+ `Code: ${ key } ; The provided arguments length ( ${ args . length } ) does not ` +
427
+ `match the required ones ( ${ msg . length } ).`
428
+ ) ;
429
+ return msg . apply ( null , args ) ;
431
430
}
432
- return String ( fmt . apply ( null , args ) ) ;
431
+
432
+ const expectedLength = ( msg . match ( / % [ d f i j o O s ] / g) || [ ] ) . length ;
433
+ internalAssert (
434
+ expectedLength === args . length ,
435
+ `Code: ${ key } ; The provided arguments length (${ args . length } ) does not ` +
436
+ `match the required ones (${ expectedLength } ).`
437
+ ) ;
438
+ if ( args . length === 0 )
439
+ return msg ;
440
+
441
+ args . unshift ( msg ) ;
442
+ return util . format . apply ( null , args ) ;
433
443
}
434
444
435
445
/**
@@ -740,7 +750,7 @@ E('ERR_HTTP2_INVALID_SETTING_VALUE',
740
750
'Invalid value for setting "%s": %s' , TypeError , RangeError ) ;
741
751
E ( 'ERR_HTTP2_INVALID_STREAM' , 'The stream has been destroyed' , Error ) ;
742
752
E ( 'ERR_HTTP2_MAX_PENDING_SETTINGS_ACK' ,
743
- 'Maximum number of pending settings acknowledgements (%s) ' , Error ) ;
753
+ 'Maximum number of pending settings acknowledgements' , Error ) ;
744
754
E ( 'ERR_HTTP2_NO_SOCKET_MANIPULATION' ,
745
755
'HTTP/2 sockets should not be directly manipulated (e.g. read and written)' ,
746
756
Error ) ;
@@ -793,7 +803,7 @@ E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
793
803
} , TypeError , RangeError ) ;
794
804
E ( 'ERR_INVALID_ARRAY_LENGTH' ,
795
805
( name , len , actual ) => {
796
- internalAssert ( typeof actual === 'number' , 'actual must be a number' ) ;
806
+ internalAssert ( typeof actual === 'number' , 'actual must be of type number' ) ;
797
807
return `The array "${ name } " (length ${ actual } ) must be of length ${ len } .` ;
798
808
} , TypeError ) ;
799
809
E ( 'ERR_INVALID_ASYNC_ID' , 'Invalid %s value: %s' , RangeError ) ;
@@ -925,7 +935,9 @@ E('ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET',
925
935
Error ) ;
926
936
E ( 'ERR_UNESCAPED_CHARACTERS' , '%s contains unescaped characters' , TypeError ) ;
927
937
E ( 'ERR_UNHANDLED_ERROR' ,
928
- ( err ) => {
938
+ // Using a default argument here is important so the argument is not counted
939
+ // towards `Function#length`.
940
+ ( err = undefined ) => {
929
941
const msg = 'Unhandled error.' ;
930
942
if ( err === undefined ) return msg ;
931
943
return `${ msg } (${ err } )` ;
@@ -960,7 +972,6 @@ E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);
960
972
E ( 'ERR_ZLIB_INITIALIZATION_FAILED' , 'Initialization failed' , Error ) ;
961
973
962
974
function invalidArgType ( name , expected , actual ) {
963
- internalAssert ( arguments . length === 3 , 'Exactly 3 arguments are required' ) ;
964
975
internalAssert ( typeof name === 'string' , 'name must be a string' ) ;
965
976
966
977
// determiner: 'must be' or 'must not be'
@@ -1007,8 +1018,7 @@ function missingArgs(...args) {
1007
1018
}
1008
1019
1009
1020
function oneOf ( expected , thing ) {
1010
- internalAssert ( expected , 'expected is required' ) ;
1011
- internalAssert ( typeof thing === 'string' , 'thing is required' ) ;
1021
+ internalAssert ( typeof thing === 'string' , '`thing` has to be of type string' ) ;
1012
1022
if ( Array . isArray ( expected ) ) {
1013
1023
const len = expected . length ;
1014
1024
internalAssert ( len > 0 ,
@@ -1027,25 +1037,28 @@ function oneOf(expected, thing) {
1027
1037
}
1028
1038
}
1029
1039
1030
- function bufferOutOfBounds ( name , isWriting ) {
1031
- if ( isWriting ) {
1032
- return 'Attempt to write outside buffer bounds' ;
1033
- } else {
1040
+ // Using a default argument here is important so the argument is not counted
1041
+ // towards `Function#length`.
1042
+ function bufferOutOfBounds ( name = undefined ) {
1043
+ if ( name ) {
1034
1044
return `"${ name } " is outside of buffer bounds` ;
1035
1045
}
1046
+ return 'Attempt to write outside buffer bounds' ;
1036
1047
}
1037
1048
1038
- function invalidChar ( name , field ) {
1049
+ // Using a default argument here is important so the argument is not counted
1050
+ // towards `Function#length`.
1051
+ function invalidChar ( name , field = undefined ) {
1039
1052
let msg = `Invalid character in ${ name } ` ;
1040
- if ( field ) {
1053
+ if ( field !== undefined ) {
1041
1054
msg += ` ["${ field } "]` ;
1042
1055
}
1043
1056
return msg ;
1044
1057
}
1045
1058
1046
1059
function outOfRange ( name , range , value ) {
1047
1060
let msg = `The value of "${ name } " is out of range.` ;
1048
- if ( range ) msg += ` It must be ${ range } .` ;
1049
- if ( value !== undefined ) msg += ` Received ${ value } ` ;
1061
+ if ( range !== undefined ) msg += ` It must be ${ range } .` ;
1062
+ msg += ` Received ${ value } ` ;
1050
1063
return msg ;
1051
1064
}
0 commit comments