| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ae3459a commit df94cfb
127 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,21 @@ const { | |||
| 26 | 26 | const messages = new Map(); | |
| 27 | 27 | const codes = {}; | |
| 28 | 28 | ||
| 29 | + const classRegExp = /^([A-Z][a-z0-9]*)+$/; | ||
| 30 | + // Sorted by a rough estimate on most frequently used entries. | ||
| 31 | + const kTypes = [ | ||
| 32 | + 'string', | ||
| 33 | + 'function', | ||
| 34 | + 'number', | ||
| 35 | + 'object', | ||
| 36 | + // Accept 'Function' and 'Object' as alternative to the lower cased version. | ||
| 37 | + 'Function', | ||
| 38 | + 'Object', | ||
| 39 | + 'boolean', | ||
| 40 | + 'bigint', | ||
| 41 | + 'symbol' | ||
| 42 | + ]; | ||
| 43 | + | ||
| 29 | 44 | const { kMaxLength } = internalBinding('buffer'); | |
| 30 | 45 | ||
| 31 | 46 | const MainContextError = Error; | |
@@ -616,26 +631,6 @@ function isStackOverflowError(err) { | |||
| 616 | 631 | err.message === maxStack_ErrorMessage; | |
| 617 | 632 | } | |
| 618 | 633 | ||
| 619 | - function oneOf(expected, thing) { | ||
| 620 | - assert(typeof thing === 'string', '`thing` has to be of type string'); | ||
| 621 | - if (ArrayIsArray(expected)) { | ||
| 622 | - const len = expected.length; | ||
| 623 | - assert(len > 0, | ||
| 624 | - 'At least one expected value needs to be specified'); | ||
| 625 | - expected = expected.map((i) => String(i)); | ||
| 626 | - if (len > 2) { | ||
| 627 | - return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or ` + | ||
| 628 | - expected[len - 1]; | ||
| 629 | - } else if (len === 2) { | ||
| 630 | - return `one of ${thing} ${expected[0]} or ${expected[1]}`; | ||
| 631 | - } else { | ||
| 632 | - return `of ${thing} ${expected[0]}`; | ||
| 633 | - } | ||
| 634 | - } else { | ||
| 635 | - return `of ${thing} ${String(expected)}`; | ||
| 636 | - } | ||
| 637 | - } | ||
| 638 | - | ||
| 639 | 634 | // Only use this for integers! Decimal numbers do not work with this function. | |
| 640 | 635 | function addNumericalSeparator(val) { | |
| 641 | 636 | let res = ''; | |
@@ -934,27 +929,114 @@ E('ERR_INVALID_ADDRESS_FAMILY', function(addressType, host, port) { | |||
| 934 | 929 | E('ERR_INVALID_ARG_TYPE', | |
| 935 | 930 | (name, expected, actual) => { | |
| 936 | 931 | assert(typeof name === 'string', "'name' must be a string"); | |
| 932 | + if (!ArrayIsArray(expected)) { | ||
| 933 | + expected = [expected]; | ||
| 934 | + } | ||
| 935 | + | ||
| 936 | + let msg = 'The '; | ||
| 937 | + if (name.endsWith(' argument')) { | ||
| 938 | + // For cases like 'first argument' | ||
| 939 | + msg += `${name} `; | ||
| 940 | + } else { | ||
| 941 | + const type = name.includes('.') ? 'property' : 'argument'; | ||
| 942 | + msg += `"${name}" ${type} `; | ||
| 943 | + } | ||
| 937 | 944 | ||
| 938 | 945 | // determiner: 'must be' or 'must not be' | |
| 939 | - let determiner; | ||
| 940 | 946 | if (typeof expected === 'string' && expected.startsWith('not ')) { | |
| 941 | - determiner = 'must not be'; | ||
| 947 | + msg += 'must not be '; | ||
| 942 | 948 | expected = expected.replace(/^not /, ''); | |
| 943 | 949 | } else { | |
| 944 | - determiner = 'must be'; | ||
| 950 | + msg += 'must be '; | ||
| 945 | 951 | } | |
| 946 | 952 | ||
| 947 | - let msg; | ||
| 948 | - if (name.endsWith(' argument')) { | ||
| 949 | - // For cases like 'first argument' | ||
| 950 | - msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`; | ||
| 951 | - } else { | ||
| 952 | - const type = name.includes('.') ? 'property' : 'argument'; | ||
| 953 | - msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`; | ||
| 953 | + const types = []; | ||
| 954 | + const instances = []; | ||
| 955 | + const other = []; | ||
| 956 | + | ||
| 957 | + for (const value of expected) { | ||
| 958 | + assert(typeof value === 'string', | ||
| 959 | + 'All expected entries have to be of type string'); | ||
| 960 | + if (kTypes.includes(value)) { | ||
| 961 | + types.push(value.toLowerCase()); | ||
| 962 | + } else if (classRegExp.test(value)) { | ||
| 963 | + instances.push(value); | ||
| 964 | + } else { | ||
| 965 | + assert(value !== 'object', | ||
| 966 | + 'The value "object" should be written as "Object"'); | ||
| 967 | + other.push(value); | ||
| 968 | + } | ||
| 954 | 969 | } | |
| 955 | 970 | ||
| 956 | - // TODO(BridgeAR): Improve the output by showing `null` and similar. | ||
| 957 | - msg += `. Received type ${typeof actual}`; | ||
| 971 | + // Special handle `object` in case other instances are allowed to outline | ||
| 972 | + // the differences between each other. | ||
| 973 | + if (instances.length > 0) { | ||
| 974 | + const pos = types.indexOf('object'); | ||
| 975 | + if (pos !== -1) { | ||
| 976 | + types.splice(pos, 1); | ||
| 977 | + instances.push('Object'); | ||
| 978 | + } | ||
| 979 | + } | ||
| 980 | + | ||
| 981 | + if (types.length > 0) { | ||
| 982 | + if (types.length > 2) { | ||
| 983 | + const last = types.pop(); | ||
| 984 | + msg += `one of type ${types.join(', ')}, or ${last}`; | ||
| 985 | + } else if (types.length === 2) { | ||
| 986 | + msg += `one of type ${types[0]} or ${types[1]}`; | ||
| 987 | + } else { | ||
| 988 | + msg += `of type ${types[0]}`; | ||
| 989 | + } | ||
| 990 | + if (instances.length > 0 || other.length > 0) | ||
| 991 | + msg += ' or '; | ||
| 992 | + } | ||
| 993 | + | ||
| 994 | + if (instances.length > 0) { | ||
| 995 | + if (instances.length > 2) { | ||
| 996 | + const last = instances.pop(); | ||
| 997 | + msg += `an instance of ${instances.join(', ')}, or ${last}`; | ||
| 998 | + } else { | ||
| 999 | + msg += `an instance of ${instances[0]}`; | ||
| 1000 | + if (instances.length === 2) { | ||
| 1001 | + msg += ` or ${instances[1]}`; | ||
| 1002 | + } | ||
| 1003 | + } | ||
| 1004 | + if (other.length > 0) | ||
| 1005 | + msg += ' or '; | ||
| 1006 | + } | ||
| 1007 | + | ||
| 1008 | + if (other.length > 0) { | ||
| 1009 | + if (other.length > 2) { | ||
| 1010 | + const last = other.pop(); | ||
| 1011 | + msg += `one of ${other.join(', ')}, or ${last}`; | ||
| 1012 | + } else if (other.length === 2) { | ||
| 1013 | + msg += `one of ${other[0]} or ${other[1]}`; | ||
| 1014 | + } else { | ||
| 1015 | + if (other[0].toLowerCase() !== other[0]) | ||
| 1016 | + msg += 'an '; | ||
| 1017 | + msg += `${other[0]}`; | ||
| 1018 | + } | ||
| 1019 | + } | ||
| 1020 | + | ||
| 1021 | + if (actual == null) { | ||
| 1022 | + msg += `. Received ${actual}`; | ||
| 1023 | + } else if (typeof actual === 'function' && actual.name) { | ||
| 1024 | + msg += `. Received function ${actual.name}`; | ||
| 1025 | + } else if (typeof actual === 'object') { | ||
| 1026 | + if (actual.constructor && actual.constructor.name) { | ||
| 1027 | + msg += `. Received an instance of ${actual.constructor.name}`; | ||
| 1028 | + } else { | ||
| 1029 | + const inspected = lazyInternalUtilInspect() | ||
| 1030 | + .inspect(actual, { depth: -1 }); | ||
| 1031 | + msg += `. Received ${inspected}`; | ||
| 1032 | + } | ||
| 1033 | + } else { | ||
| 1034 | + let inspected = lazyInternalUtilInspect() | ||
| 1035 | + .inspect(actual, { colors: false }); | ||
| 1036 | + if (inspected.length > 25) | ||
| 1037 | + inspected = `${inspected.slice(0, 25)}...`; | ||
| 1038 | + msg += `. Received type ${typeof actual} (${inspected})`; | ||
| 1039 | + } | ||
| 958 | 1040 | return msg; | |
| 959 | 1041 | }, TypeError); | |
| 960 | 1042 | E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => { | |
@@ -1042,7 +1124,15 @@ E('ERR_INVALID_URL', function(input) { | |||
| 1042 | 1124 | return `Invalid URL: ${input}`; | |
| 1043 | 1125 | }, TypeError); | |
| 1044 | 1126 | E('ERR_INVALID_URL_SCHEME', | |
| 1045 | - (expected) => `The URL must be ${oneOf(expected, 'scheme')}`, TypeError); | ||
| 1127 | + (expected) => { | ||
| 1128 | + if (typeof expected === 'string') | ||
| 1129 | + expected = [expected]; | ||
| 1130 | + assert(expected.length <= 2); | ||
| 1131 | + const res = expected.length === 2 ? | ||
| 1132 | + `one of scheme ${expected[0]} or ${expected[1]}` : | ||
| 1133 | + `of scheme ${expected[0]}`; | ||
| 1134 | + return `The URL must be ${res}`; | ||
| 1135 | + }, TypeError); | ||
| 1046 | 1136 | E('ERR_IPC_CHANNEL_CLOSED', 'Channel closed', Error); | |
| 1047 | 1137 | E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected', Error); | |
| 1048 | 1138 | E('ERR_IPC_ONE_PIPE', 'Child process can have only one IPC pipe', Error); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -718,6 +718,26 @@ function runWithInvalidFD(func) { | |||
| 718 | 718 | printSkipMessage('Could not generate an invalid fd'); | |
| 719 | 719 | } | |
| 720 | 720 | ||
| 721 | + // A helper function to simplify checking for ERR_INVALID_ARG_TYPE output. | ||
| 722 | + function invalidArgTypeHelper(input) { | ||
| 723 | + if (input == null) { | ||
| 724 | + return ` Received ${input}`; | ||
| 725 | + } | ||
| 726 | + if (typeof input === 'function' && input.name) { | ||
| 727 | + return ` Received function ${input.name}`; | ||
| 728 | + } | ||
| 729 | + if (typeof input === 'object') { | ||
| 730 | + if (input.constructor && input.constructor.name) { | ||
| 731 | + return ` Received an instance of ${input.constructor.name}`; | ||
| 732 | + } | ||
| 733 | + return ` Received ${util.inspect(input, { depth: -1 })}`; | ||
| 734 | + } | ||
| 735 | + let inspected = util.inspect(input, { colors: false }); | ||
| 736 | + if (inspected.length > 25) | ||
| 737 | + inspected = `${inspected.slice(0, 25)}...`; | ||
| 738 | + return ` Received type ${typeof input} (${inspected})`; | ||
| 739 | + } | ||
| 740 | + | ||
| 721 | 741 | module.exports = { | |
| 722 | 742 | allowGlobals, | |
| 723 | 743 | buildType, | |
@@ -735,6 +755,7 @@ module.exports = { | |||
| 735 | 755 | hasIntl, | |
| 736 | 756 | hasCrypto, | |
| 737 | 757 | hasMultiLocalhost, | |
| 758 | + invalidArgTypeHelper, | ||
| 738 | 759 | isAIX, | |
| 739 | 760 | isAlive, | |
| 740 | 761 | isFreeBSD, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,7 +25,8 @@ common.expectsError( | |||
| 25 | 25 | { | |
| 26 | 26 | code: 'ERR_INVALID_ARG_TYPE', | |
| 27 | 27 | type: TypeError, | |
| 28 | - message: 'The "url" argument must be of type string. Received type number' | ||
| 28 | + message: 'The "url" argument must be of type string. Received type number' + | ||
| 29 | + ' (1)' | ||
| 29 | 30 | } | |
| 30 | 31 | ); | |
| 31 | 32 | ||
@@ -34,7 +35,8 @@ common.expectsError( | |||
| 34 | 35 | { | |
| 35 | 36 | code: 'ERR_INVALID_ARG_TYPE', | |
| 36 | 37 | type: TypeError, | |
| 37 | - message: 'The "url" argument must be of type string. Received type number' | ||
| 38 | + message: 'The "url" argument must be of type string. Received type number' + | ||
| 39 | + ' (1)' | ||
| 38 | 40 | } | |
| 39 | 41 | ); | |
| 40 | 42 | ||
@@ -43,8 +45,8 @@ common.expectsError( | |||
| 43 | 45 | { | |
| 44 | 46 | code: 'ERR_INVALID_ARG_TYPE', | |
| 45 | 47 | type: TypeError, | |
| 46 | - message: 'The "job" argument must be of type ModuleJob. ' + | ||
| 47 | - 'Received type string' | ||
| 48 | + message: 'The "job" argument must be an instance of ModuleJob. ' + | ||
| 49 | + "Received type string ('notamodulejob')" | ||
| 48 | 50 | } | |
| 49 | 51 | ); | |
| 50 | 52 | ||
@@ -53,6 +55,7 @@ common.expectsError( | |||
| 53 | 55 | { | |
| 54 | 56 | code: 'ERR_INVALID_ARG_TYPE', | |
| 55 | 57 | type: TypeError, | |
| 56 | - message: 'The "url" argument must be of type string. Received type number' | ||
| 58 | + message: 'The "url" argument must be of type string. Received type number' + | ||
| 59 | + ' (1)' | ||
| 57 | 60 | } | |
| 58 | 61 | ); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,7 +26,7 @@ const dnsPromises = require('dns').promises; | |||
| 26 | 26 | code: 'ERR_INVALID_ARG_TYPE', | |
| 27 | 27 | type: TypeError, | |
| 28 | 28 | message: 'The "rrtype" argument must be of type string. ' + | |
| 29 | - `Received type ${typeof rrtype}` | ||
| 29 | + `Received type ${typeof rrtype} (${rrtype})` | ||
| 30 | 30 | } | |
| 31 | 31 | ); | |
| 32 | 32 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -109,8 +109,8 @@ promises.push(assert.rejects( | |||
| 109 | 109 | assert.rejects('fail', {}), | |
| 110 | 110 | { | |
| 111 | 111 | code: 'ERR_INVALID_ARG_TYPE', | |
| 112 | - message: 'The "promiseFn" argument must be one of type ' + | ||
| 113 | - 'Function or Promise. Received type string' | ||
| 112 | + message: 'The "promiseFn" argument must be of type function or an ' + | ||
| 113 | + "instance of Promise. Received type string ('fail')" | ||
| 114 | 114 | } | |
| 115 | 115 | )); | |
| 116 | 116 | ||
@@ -209,8 +209,8 @@ promises.push(assert.rejects( | |||
| 209 | 209 | assert.doesNotReject(123), | |
| 210 | 210 | { | |
| 211 | 211 | code: 'ERR_INVALID_ARG_TYPE', | |
| 212 | - message: 'The "promiseFn" argument must be one of type ' + | ||
| 213 | - 'Function or Promise. Received type number' | ||
| 212 | + message: 'The "promiseFn" argument must be of type ' + | ||
| 213 | + 'function or an instance of Promise. Received type number (123)' | ||
| 214 | 214 | } | |
| 215 | 215 | )); | |
| 216 | 216 | /* eslint-enable no-restricted-syntax */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -403,8 +403,8 @@ assert.throws( | |||
| 403 | 403 | { | |
| 404 | 404 | code: 'ERR_INVALID_ARG_TYPE', | |
| 405 | 405 | type: TypeError, | |
| 406 | - message: 'The "fn" argument must be of type Function. Received ' + | ||
| 407 | - `type ${typeof fn}` | ||
| 406 | + message: 'The "fn" argument must be of type function.' + | ||
| 407 | + common.invalidArgTypeHelper(fn) | ||
| 408 | 408 | } | |
| 409 | 409 | ); | |
| 410 | 410 | }; | |
@@ -473,8 +473,8 @@ assert.throws(() => { | |||
| 473 | 473 | { | |
| 474 | 474 | code: 'ERR_INVALID_ARG_TYPE', | |
| 475 | 475 | name: 'TypeError', | |
| 476 | - message: 'The "options" argument must be of type Object. ' + | ||
| 477 | - `Received type ${typeof input}` | ||
| 476 | + message: 'The "options" argument must be of type object.' + | ||
| 477 | + common.invalidArgTypeHelper(input) | ||
| 478 | 478 | }); | |
| 479 | 479 | }); | |
| 480 | 480 | } | |
@@ -920,8 +920,9 @@ common.expectsError( | |||
| 920 | 920 | { | |
| 921 | 921 | code: 'ERR_INVALID_ARG_TYPE', | |
| 922 | 922 | type: TypeError, | |
| 923 | - message: 'The "error" argument must be one of type Object, Error, ' + | ||
| 924 | - 'Function, or RegExp. Received type string' | ||
| 923 | + message: 'The "error" argument must be of type function or ' + | ||
| 924 | + 'an instance of Error, RegExp, or Object. Received type string ' + | ||
| 925 | + "('Error message')" | ||
| 925 | 926 | } | |
| 926 | 927 | ); | |
| 927 | 928 | ||
@@ -934,8 +935,9 @@ common.expectsError( | |||
| 934 | 935 | () => assert.throws(() => {}, input), | |
| 935 | 936 | { | |
| 936 | 937 | code: 'ERR_INVALID_ARG_TYPE', | |
| 937 | - message: 'The "error" argument must be one of type Object, Error, ' + | ||
| 938 | - `Function, or RegExp. Received type ${typeof input}` | ||
| 938 | + message: 'The "error" argument must be of type function or ' + | ||
| 939 | + 'an instance of Error, RegExp, or Object.' + | ||
| 940 | + common.invalidArgTypeHelper(input) | ||
| 939 | 941 | } | |
| 940 | 942 | ); | |
| 941 | 943 | }); | |
@@ -1013,8 +1015,8 @@ common.expectsError( | |||
| 1013 | 1015 | { | |
| 1014 | 1016 | type: TypeError, | |
| 1015 | 1017 | code: 'ERR_INVALID_ARG_TYPE', | |
| 1016 | - message: 'The "expected" argument must be one of type Function or ' + | ||
| 1017 | - 'RegExp. Received type object' | ||
| 1018 | + message: 'The "expected" argument must be of type function or an ' + | ||
| 1019 | + 'instance of RegExp. Received an instance of Object' | ||
| 1018 | 1020 | } | |
| 1019 | 1021 | ); | |
| 1020 | 1022 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -967,19 +967,19 @@ common.expectsError( | |||
| 967 | 967 | { | |
| 968 | 968 | code: 'ERR_INVALID_ARG_TYPE', | |
| 969 | 969 | type: TypeError, | |
| 970 | - message: 'The "target" argument must be one of type Buffer or Uint8Array.' + | ||
| 971 | - ' Received type undefined' | ||
| 970 | + message: 'The "target" argument must be an instance of Buffer or ' + | ||
| 971 | + 'Uint8Array. Received undefined' | ||
| 972 | 972 | }); | |
| 973 | 973 | ||
| 974 | 974 | assert.throws(() => Buffer.from(), { | |
| 975 | 975 | name: 'TypeError', | |
| 976 | - message: 'The first argument must be one of type string, Buffer, ' + | ||
| 977 | - 'ArrayBuffer, Array, or Array-like Object. Received type undefined' | ||
| 976 | + message: 'The first argument must be of type string or an instance of ' + | ||
| 977 | + 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined' | ||
| 978 | 978 | }); | |
| 979 | 979 | assert.throws(() => Buffer.from(null), { | |
| 980 | 980 | name: 'TypeError', | |
| 981 | - message: 'The first argument must be one of type string, Buffer, ' + | ||
| 982 | - 'ArrayBuffer, Array, or Array-like Object. Received type object' | ||
| 981 | + message: 'The first argument must be of type string or an instance of ' + | ||
| 982 | + 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received null' | ||
| 983 | 983 | }); | |
| 984 | 984 | ||
| 985 | 985 | // Test prototype getters don't throw | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,8 +43,9 @@ assert.throws(function() { | |||
| 43 | 43 | }, { | |
| 44 | 44 | code: 'ERR_INVALID_ARG_TYPE', | |
| 45 | 45 | name: 'TypeError', | |
| 46 | - message: 'The first argument must be one of type string, Buffer,' + | ||
| 47 | - ' ArrayBuffer, Array, or Array-like Object. Received type object' | ||
| 46 | + message: 'The first argument must be of type string or an instance of ' + | ||
| 47 | + 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received ' + | ||
| 48 | + 'an instance of AB' | ||
| 48 | 49 | }); | |
| 49 | 50 | ||
| 50 | 51 | // Test the byteOffset and length arguments | |
| Back | FazBrowse Home | New Git URL |
0 commit comments