@@ -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
/**
@@ -739,7 +749,7 @@ E('ERR_HTTP2_INVALID_SETTING_VALUE',
739
749
'Invalid value for setting "%s": %s' , TypeError , RangeError ) ;
740
750
E ( 'ERR_HTTP2_INVALID_STREAM' , 'The stream has been destroyed' , Error ) ;
741
751
E ( 'ERR_HTTP2_MAX_PENDING_SETTINGS_ACK' ,
742
- 'Maximum number of pending settings acknowledgements (%s) ' , Error ) ;
752
+ 'Maximum number of pending settings acknowledgements' , Error ) ;
743
753
E ( 'ERR_HTTP2_NO_SOCKET_MANIPULATION' ,
744
754
'HTTP/2 sockets should not be directly manipulated (e.g. read and written)' ,
745
755
Error ) ;
@@ -792,7 +802,7 @@ E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
792
802
} , TypeError , RangeError ) ;
793
803
E ( 'ERR_INVALID_ARRAY_LENGTH' ,
794
804
( name , len , actual ) => {
795
- internalAssert ( typeof actual === 'number' , 'actual must be a number' ) ;
805
+ internalAssert ( typeof actual === 'number' , 'actual must be of type number' ) ;
796
806
return `The array "${ name } " (length ${ actual } ) must be of length ${ len } .` ;
797
807
} , TypeError ) ;
798
808
E ( 'ERR_INVALID_ASYNC_ID' , 'Invalid %s value: %s' , RangeError ) ;
@@ -924,7 +934,7 @@ E('ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET',
924
934
Error ) ;
925
935
E ( 'ERR_UNESCAPED_CHARACTERS' , '%s contains unescaped characters' , TypeError ) ;
926
936
E ( 'ERR_UNHANDLED_ERROR' ,
927
- ( err ) => {
937
+ ( err = undefined ) => {
928
938
const msg = 'Unhandled error.' ;
929
939
if ( err === undefined ) return msg ;
930
940
return `${ msg } (${ err } )` ;
@@ -959,7 +969,6 @@ E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);
959
969
E ( 'ERR_ZLIB_INITIALIZATION_FAILED' , 'Initialization failed' , Error ) ;
960
970
961
971
function invalidArgType ( name , expected , actual ) {
962
- internalAssert ( arguments . length === 3 , 'Exactly 3 arguments are required' ) ;
963
972
internalAssert ( typeof name === 'string' , 'name must be a string' ) ;
964
973
965
974
// determiner: 'must be' or 'must not be'
@@ -1006,8 +1015,7 @@ function missingArgs(...args) {
1006
1015
}
1007
1016
1008
1017
function oneOf ( expected , thing ) {
1009
- internalAssert ( expected , 'expected is required' ) ;
1010
- internalAssert ( typeof thing === 'string' , 'thing is required' ) ;
1018
+ internalAssert ( typeof thing === 'string' , '`thing` has to be of type string' ) ;
1011
1019
if ( Array . isArray ( expected ) ) {
1012
1020
const len = expected . length ;
1013
1021
internalAssert ( len > 0 ,
@@ -1026,25 +1034,24 @@ function oneOf(expected, thing) {
1026
1034
}
1027
1035
}
1028
1036
1029
- function bufferOutOfBounds ( name , isWriting ) {
1030
- if ( isWriting ) {
1031
- return 'Attempt to write outside buffer bounds' ;
1032
- } else {
1037
+ function bufferOutOfBounds ( name = undefined ) {
1038
+ if ( name ) {
1033
1039
return `"${ name } " is outside of buffer bounds` ;
1034
1040
}
1041
+ return 'Attempt to write outside buffer bounds' ;
1035
1042
}
1036
1043
1037
- function invalidChar ( name , field ) {
1044
+ function invalidChar ( name , field = undefined ) {
1038
1045
let msg = `Invalid character in ${ name } ` ;
1039
- if ( field ) {
1046
+ if ( field !== undefined ) {
1040
1047
msg += ` ["${ field } "]` ;
1041
1048
}
1042
1049
return msg ;
1043
1050
}
1044
1051
1045
1052
function outOfRange ( name , range , value ) {
1046
1053
let msg = `The value of "${ name } " is out of range.` ;
1047
- if ( range ) msg += ` It must be ${ range } .` ;
1048
- if ( value !== undefined ) msg += ` Received ${ value } ` ;
1054
+ if ( range !== undefined ) msg += ` It must be ${ range } .` ;
1055
+ msg += ` Received ${ value } ` ;
1049
1056
return msg ;
1050
1057
}
0 commit comments