@@ -416,20 +416,30 @@ function internalAssert(condition, message) {
416416 }
417417}
418418
419- function message ( key , args ) {
419+ function message ( key , args = [ ] ) {
420420 const msg = messages . get ( key ) ;
421- internalAssert ( msg , `An invalid error message key was used: ${ key } .` ) ;
422- let fmt ;
423421 if ( util === undefined ) util = require ( 'util' ) ;
422+
424423 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 ) ;
431430 }
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 ) ;
433443}
434444
435445/**
@@ -739,7 +749,7 @@ E('ERR_HTTP2_INVALID_SETTING_VALUE',
739749 'Invalid value for setting "%s": %s' , TypeError , RangeError ) ;
740750E ( 'ERR_HTTP2_INVALID_STREAM' , 'The stream has been destroyed' , Error ) ;
741751E ( 'ERR_HTTP2_MAX_PENDING_SETTINGS_ACK' ,
742- 'Maximum number of pending settings acknowledgements (%s) ' , Error ) ;
752+ 'Maximum number of pending settings acknowledgements' , Error ) ;
743753E ( 'ERR_HTTP2_NO_SOCKET_MANIPULATION' ,
744754 'HTTP/2 sockets should not be directly manipulated (e.g. read and written)' ,
745755 Error ) ;
@@ -792,7 +802,7 @@ E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
792802} , TypeError , RangeError ) ;
793803E ( 'ERR_INVALID_ARRAY_LENGTH' ,
794804 ( name , len , actual ) => {
795- internalAssert ( typeof actual === 'number' , 'actual must be a number' ) ;
805+ internalAssert ( typeof actual === 'number' , 'actual must be of type number' ) ;
796806 return `The array "${ name } " (length ${ actual } ) must be of length ${ len } .` ;
797807 } , TypeError ) ;
798808E ( 'ERR_INVALID_ASYNC_ID' , 'Invalid %s value: %s' , RangeError ) ;
@@ -924,7 +934,7 @@ E('ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET',
924934 Error ) ;
925935E ( 'ERR_UNESCAPED_CHARACTERS' , '%s contains unescaped characters' , TypeError ) ;
926936E ( 'ERR_UNHANDLED_ERROR' ,
927- ( err ) => {
937+ ( err = undefined ) => {
928938 const msg = 'Unhandled error.' ;
929939 if ( err === undefined ) return msg ;
930940 return `${ msg } (${ err } )` ;
@@ -959,7 +969,6 @@ E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);
959969E ( 'ERR_ZLIB_INITIALIZATION_FAILED' , 'Initialization failed' , Error ) ;
960970
961971function invalidArgType ( name , expected , actual ) {
962- internalAssert ( arguments . length === 3 , 'Exactly 3 arguments are required' ) ;
963972 internalAssert ( typeof name === 'string' , 'name must be a string' ) ;
964973
965974 // determiner: 'must be' or 'must not be'
@@ -1006,8 +1015,7 @@ function missingArgs(...args) {
10061015}
10071016
10081017function 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' ) ;
10111019 if ( Array . isArray ( expected ) ) {
10121020 const len = expected . length ;
10131021 internalAssert ( len > 0 ,
@@ -1026,25 +1034,24 @@ function oneOf(expected, thing) {
10261034 }
10271035}
10281036
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 ) {
10331039 return `"${ name } " is outside of buffer bounds` ;
10341040 }
1041+ return 'Attempt to write outside buffer bounds' ;
10351042}
10361043
1037- function invalidChar ( name , field ) {
1044+ function invalidChar ( name , field = undefined ) {
10381045 let msg = `Invalid character in ${ name } ` ;
1039- if ( field ) {
1046+ if ( field !== undefined ) {
10401047 msg += ` ["${ field } "]` ;
10411048 }
10421049 return msg ;
10431050}
10441051
10451052function outOfRange ( name , range , value ) {
10461053 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 } ` ;
10491056 return msg ;
10501057}
0 commit comments