Skip to content

Commit 4c9c77d

Browse files
committed
errors: move functions to error code
This makes sure the functions are actually directly beneat the specification of an error code. That way it is not necessary to jump around when looking at the functionality.
1 parent 9fce095 commit 4c9c77d

File tree

1 file changed

+92
-97
lines changed

1 file changed

+92
-97
lines changed

lib/internal/errors.js

Lines changed: 92 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,26 @@ function isStackOverflowError(err) {
407407
err.message === maxStack_ErrorMessage;
408408
}
409409

410+
function oneOf(expected, thing) {
411+
assert(typeof thing === 'string', '`thing` has to be of type string');
412+
if (Array.isArray(expected)) {
413+
const len = expected.length;
414+
assert(len > 0,
415+
'At least one expected value needs to be specified');
416+
expected = expected.map((i) => String(i));
417+
if (len > 2) {
418+
return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or ` +
419+
expected[len - 1];
420+
} else if (len === 2) {
421+
return `one of ${thing} ${expected[0]} or ${expected[1]}`;
422+
} else {
423+
return `of ${thing} ${expected[0]}`;
424+
}
425+
} else {
426+
return `of ${thing} ${String(expected)}`;
427+
}
428+
}
429+
410430
module.exports = {
411431
dnsException,
412432
errnoException,
@@ -441,7 +461,15 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError);
441461
E('ERR_ASSERTION', '%s', Error);
442462
E('ERR_ASYNC_CALLBACK', '%s must be a function', TypeError);
443463
E('ERR_ASYNC_TYPE', 'Invalid name for async "type": %s', TypeError);
444-
E('ERR_BUFFER_OUT_OF_BOUNDS', bufferOutOfBounds, RangeError);
464+
E('ERR_BUFFER_OUT_OF_BOUNDS',
465+
// Using a default argument here is important so the argument is not counted
466+
// towards `Function#length`.
467+
(name = undefined) => {
468+
if (name) {
469+
return `"${name}" is outside of buffer bounds`;
470+
}
471+
return 'Attempt to write outside buffer bounds';
472+
}, RangeError);
445473
E('ERR_BUFFER_TOO_LARGE',
446474
`Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes`,
447475
RangeError);
@@ -579,7 +607,32 @@ E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
579607
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
580608
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
581609
E('ERR_INVALID_ADDRESS_FAMILY', 'Invalid address family: %s', RangeError);
582-
E('ERR_INVALID_ARG_TYPE', invalidArgType, TypeError);
610+
E('ERR_INVALID_ARG_TYPE',
611+
(name, expected, actual) => {
612+
assert(typeof name === 'string', "'name' must be a string");
613+
614+
// determiner: 'must be' or 'must not be'
615+
let determiner;
616+
if (typeof expected === 'string' && expected.startsWith('not ')) {
617+
determiner = 'must not be';
618+
expected = expected.replace(/^not /, '');
619+
} else {
620+
determiner = 'must be';
621+
}
622+
623+
let msg;
624+
if (name.endsWith(' argument')) {
625+
// For cases like 'first argument'
626+
msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
627+
} else {
628+
const type = name.includes('.') ? 'property' : 'argument';
629+
msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
630+
}
631+
632+
// TODO(BridgeAR): Improve the output by showing `null` and similar.
633+
msg += `. Received type ${typeof actual}`;
634+
return msg;
635+
}, TypeError);
583636
E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
584637
let inspected = util.inspect(value);
585638
if (inspected.length > 128) {
@@ -595,7 +648,16 @@ E('ERR_INVALID_ASYNC_ID', 'Invalid %s value: %s', RangeError);
595648
E('ERR_INVALID_BUFFER_SIZE',
596649
'Buffer size must be a multiple of %s', RangeError);
597650
E('ERR_INVALID_CALLBACK', 'Callback must be a function', TypeError);
598-
E('ERR_INVALID_CHAR', invalidChar, TypeError);
651+
E('ERR_INVALID_CHAR',
652+
// Using a default argument here is important so the argument is not counted
653+
// towards `Function#length`.
654+
(name, field = undefined) => {
655+
let msg = `Invalid character in ${name}`;
656+
if (field !== undefined) {
657+
msg += ` ["${field}"]`;
658+
}
659+
return msg;
660+
}, TypeError);
599661
E('ERR_INVALID_CURSOR_POS',
600662
'Cannot set cursor row without setting its column', TypeError);
601663
E('ERR_INVALID_FD',
@@ -644,7 +706,26 @@ E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected', Error);
644706
E('ERR_IPC_ONE_PIPE', 'Child process can have only one IPC pipe', Error);
645707
E('ERR_IPC_SYNC_FORK', 'IPC cannot be used with synchronous forks', Error);
646708
E('ERR_METHOD_NOT_IMPLEMENTED', 'The %s method is not implemented', Error);
647-
E('ERR_MISSING_ARGS', missingArgs, TypeError);
709+
E('ERR_MISSING_ARGS',
710+
(...args) => {
711+
assert(args.length > 0, 'At least one arg needs to be specified');
712+
let msg = 'The ';
713+
const len = args.length;
714+
args = args.map((a) => `"${a}"`);
715+
switch (len) {
716+
case 1:
717+
msg += `${args[0]} argument`;
718+
break;
719+
case 2:
720+
msg += `${args[0]} and ${args[1]} arguments`;
721+
break;
722+
default:
723+
msg += args.slice(0, len - 1).join(', ');
724+
msg += `, and ${args[len - 1]} arguments`;
725+
break;
726+
}
727+
return `${msg} must be specified`;
728+
}, TypeError);
648729
E('ERR_MISSING_MODULE', 'Cannot find module %s', Error);
649730
E('ERR_MODULE_RESOLUTION_LEGACY',
650731
'%s not found by import in %s.' +
@@ -665,7 +746,13 @@ E('ERR_NO_CRYPTO',
665746
E('ERR_NO_ICU',
666747
'%s is not supported on Node.js compiled without ICU', TypeError);
667748
E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported', Error);
668-
E('ERR_OUT_OF_RANGE', outOfRange, RangeError);
749+
E('ERR_OUT_OF_RANGE',
750+
(name, range, value) => {
751+
let msg = `The value of "${name}" is out of range.`;
752+
if (range !== undefined) msg += ` It must be ${range}.`;
753+
msg += ` Received ${value}`;
754+
return msg;
755+
}, RangeError);
669756
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);
670757
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
671758
'Script execution was interrupted by `SIGINT`', Error);
@@ -762,95 +849,3 @@ E('ERR_VM_MODULE_NOT_MODULE',
762849
'Provided module is not an instance of Module', Error);
763850
E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);
764851
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed', Error);
765-
766-
function invalidArgType(name, expected, actual) {
767-
assert(typeof name === 'string', "'name' must be a string");
768-
769-
// determiner: 'must be' or 'must not be'
770-
let determiner;
771-
if (typeof expected === 'string' && expected.startsWith('not ')) {
772-
determiner = 'must not be';
773-
expected = expected.replace(/^not /, '');
774-
} else {
775-
determiner = 'must be';
776-
}
777-
778-
let msg;
779-
if (name.endsWith(' argument')) {
780-
// For cases like 'first argument'
781-
msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
782-
} else {
783-
const type = name.includes('.') ? 'property' : 'argument';
784-
msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
785-
}
786-
787-
// TODO(BridgeAR): Improve the output by showing `null` and similar.
788-
msg += `. Received type ${typeof actual}`;
789-
return msg;
790-
}
791-
792-
function missingArgs(...args) {
793-
assert(args.length > 0, 'At least one arg needs to be specified');
794-
let msg = 'The ';
795-
const len = args.length;
796-
args = args.map((a) => `"${a}"`);
797-
switch (len) {
798-
case 1:
799-
msg += `${args[0]} argument`;
800-
break;
801-
case 2:
802-
msg += `${args[0]} and ${args[1]} arguments`;
803-
break;
804-
default:
805-
msg += args.slice(0, len - 1).join(', ');
806-
msg += `, and ${args[len - 1]} arguments`;
807-
break;
808-
}
809-
return `${msg} must be specified`;
810-
}
811-
812-
function oneOf(expected, thing) {
813-
assert(typeof thing === 'string', '`thing` has to be of type string');
814-
if (Array.isArray(expected)) {
815-
const len = expected.length;
816-
assert(len > 0,
817-
'At least one expected value needs to be specified');
818-
expected = expected.map((i) => String(i));
819-
if (len > 2) {
820-
return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or ` +
821-
expected[len - 1];
822-
} else if (len === 2) {
823-
return `one of ${thing} ${expected[0]} or ${expected[1]}`;
824-
} else {
825-
return `of ${thing} ${expected[0]}`;
826-
}
827-
} else {
828-
return `of ${thing} ${String(expected)}`;
829-
}
830-
}
831-
832-
// Using a default argument here is important so the argument is not counted
833-
// towards `Function#length`.
834-
function bufferOutOfBounds(name = undefined) {
835-
if (name) {
836-
return `"${name}" is outside of buffer bounds`;
837-
}
838-
return 'Attempt to write outside buffer bounds';
839-
}
840-
841-
// Using a default argument here is important so the argument is not counted
842-
// towards `Function#length`.
843-
function invalidChar(name, field = undefined) {
844-
let msg = `Invalid character in ${name}`;
845-
if (field !== undefined) {
846-
msg += ` ["${field}"]`;
847-
}
848-
return msg;
849-
}
850-
851-
function outOfRange(name, range, value) {
852-
let msg = `The value of "${name}" is out of range.`;
853-
if (range !== undefined) msg += ` It must be ${range}.`;
854-
msg += ` Received ${value}`;
855-
return msg;
856-
}

0 commit comments

Comments
 (0)