Skip to content

Commit c244436

Browse files
BridgeARtargos
authored andcommitted
errors: move functions to error code
This makes sure the functions are actually directly beneath the specification of an error code. That way it is not necessary to jump around when looking at the functionality. PR-URL: #20486 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 5d06c1e commit c244436

1 file changed

Lines changed: 92 additions & 97 deletions

File tree

‎lib/internal/errors.js‎

Lines changed: 92 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,26 @@ function isStackOverflowError(err) {
410410
err.message===maxStack_ErrorMessage;
411411
}
412412

413+
functiononeOf(expected,thing){
414+
assert(typeofthing==='string','`thing` has to be of type string');
415+
if(Array.isArray(expected)){
416+
constlen=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+
}elseif(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+
413433
module.exports={
414434
dnsException,
415435
errnoException,
@@ -444,7 +464,15 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError);
444464
E('ERR_ASSERTION','%s',Error);
445465
E('ERR_ASYNC_CALLBACK','%s must be a function',TypeError);
446466
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);
448476
E('ERR_BUFFER_TOO_LARGE',
449477
`Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes`,
450478
RangeError);
@@ -582,7 +610,32 @@ E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
582610
E('ERR_INSPECTOR_NOT_AVAILABLE','Inspector is not available',Error);
583611
E('ERR_INSPECTOR_NOT_CONNECTED','Session is not connected',Error);
584612
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(typeofname==='string',"'name' must be a string");
616+
617+
// determiner: 'must be' or 'must not be'
618+
letdeterminer;
619+
if(typeofexpected==='string'&&expected.startsWith('not ')){
620+
determiner='must not be';
621+
expected=expected.replace(/^not/,'');
622+
}else{
623+
determiner='must be';
624+
}
625+
626+
letmsg;
627+
if(name.endsWith(' argument')){
628+
// For cases like 'first argument'
629+
msg=`The ${name}${determiner}${oneOf(expected,'type')}`;
630+
}else{
631+
consttype=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 ${typeofactual}`;
637+
returnmsg;
638+
},TypeError);
586639
E('ERR_INVALID_ARG_VALUE',(name,value,reason='is invalid')=>{
587640
letinspected=util.inspect(value);
588641
if(inspected.length>128){
@@ -598,7 +651,16 @@ E('ERR_INVALID_ASYNC_ID', 'Invalid %s value: %s', RangeError);
598651
E('ERR_INVALID_BUFFER_SIZE',
599652
'Buffer size must be a multiple of %s',RangeError);
600653
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+
letmsg=`Invalid character in ${name}`;
659+
if(field!==undefined){
660+
msg+=` ["${field}"]`;
661+
}
662+
returnmsg;
663+
},TypeError);
602664
E('ERR_INVALID_CURSOR_POS',
603665
'Cannot set cursor row without setting its column',TypeError);
604666
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);
648710
E('ERR_IPC_ONE_PIPE','Child process can have only one IPC pipe',Error);
649711
E('ERR_IPC_SYNC_FORK','IPC cannot be used with synchronous forks',Error);
650712
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+
letmsg='The ';
717+
constlen=args.length;
718+
args=args.map((a)=>`"${a}"`);
719+
switch(len){
720+
case1:
721+
msg+=`${args[0]} argument`;
722+
break;
723+
case2:
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);
652733
E('ERR_MISSING_MODULE','Cannot find module %s',Error);
653734
E('ERR_MODULE_RESOLUTION_LEGACY',
654735
'%s not found by import in %s.'+
@@ -669,7 +750,13 @@ E('ERR_NO_CRYPTO',
669750
E('ERR_NO_ICU',
670751
'%s is not supported on Node.js compiled without ICU',TypeError);
671752
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+
letmsg=`The value of "${name}" is out of range.`;
756+
if(range!==undefined)msg+=` It must be ${range}.`;
757+
msg+=` Received ${value}`;
758+
returnmsg;
759+
},RangeError);
673760
E('ERR_REQUIRE_ESM','Must use import to load ES Module: %s',Error);
674761
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
675762
'Script execution was interrupted by `SIGINT`.',Error);
@@ -765,95 +852,3 @@ E('ERR_VM_MODULE_NOT_MODULE',
765852
'Provided module is not an instance of Module',Error);
766853
E('ERR_VM_MODULE_STATUS','Module status %s',Error);
767854
E('ERR_ZLIB_INITIALIZATION_FAILED','Initialization failed',Error);
768-
769-
functioninvalidArgType(name,expected,actual){
770-
assert(typeofname==='string',"'name' must be a string");
771-
772-
// determiner: 'must be' or 'must not be'
773-
letdeterminer;
774-
if(typeofexpected==='string'&&expected.startsWith('not ')){
775-
determiner='must not be';
776-
expected=expected.replace(/^not/,'');
777-
}else{
778-
determiner='must be';
779-
}
780-
781-
letmsg;
782-
if(name.endsWith(' argument')){
783-
// For cases like 'first argument'
784-
msg=`The ${name}${determiner}${oneOf(expected,'type')}`;
785-
}else{
786-
consttype=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 ${typeofactual}`;
792-
returnmsg;
793-
}
794-
795-
functionmissingArgs(...args){
796-
assert(args.length>0,'At least one arg needs to be specified');
797-
letmsg='The ';
798-
constlen=args.length;
799-
args=args.map((a)=>`"${a}"`);
800-
switch(len){
801-
case1:
802-
msg+=`${args[0]} argument`;
803-
break;
804-
case2:
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-
functiononeOf(expected,thing){
816-
assert(typeofthing==='string','`thing` has to be of type string');
817-
if(Array.isArray(expected)){
818-
constlen=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-
}elseif(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-
functionbufferOutOfBounds(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-
functioninvalidChar(name,field=undefined){
847-
letmsg=`Invalid character in ${name}`;
848-
if(field!==undefined){
849-
msg+=` ["${field}"]`;
850-
}
851-
returnmsg;
852-
}
853-
854-
functionoutOfRange(name,range,value){
855-
letmsg=`The value of "${name}" is out of range.`;
856-
if(range!==undefined)msg+=` It must be ${range}.`;
857-
msg+=` Received ${value}`;
858-
returnmsg;
859-
}

0 commit comments

Comments
 (0)