summaryrefslogtreecommitdiff
path: root/test/common/index.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-09-23 08:17:25 +0200
committerBeth Griggs <Bethany.Griggs@uk.ibm.com>2020-02-06 02:49:32 +0000
commitdf94cfb67c40768b7d5999bb164cfbea58988ffc (patch)
tree886c74c00d56fc223f2f266bc26ee3771f94a4c4 /test/common/index.js
parentae3459af9f1a1026ba25b7cde3a80357c73d6d39 (diff)
downloadnode-new-df94cfb67c40768b7d5999bb164cfbea58988ffc.tar.gz
errors: improve ERR_INVALID_ARG_TYPE
ERR_INVALID_ARG_TYPE is the most common error used throughout the code base. This improves the error message by providing more details to the user and by indicating more precisely which values are allowed ones and which ones are not. It adds the actual input to the error message in case it's a primitive. If it's a class instance, it'll print the class name instead of "object" and "falsy" or similar entries are not named "type" anymore. PR-URL: https://github.com/nodejs/node/pull/29675 Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test/common/index.js')
-rw-r--r--test/common/index.js21
1 files changed, 21 insertions, 0 deletions
diff --git a/test/common/index.js b/test/common/index.js
index dd29927759..e93c73820a 100644
--- a/test/common/index.js
+++ b/test/common/index.js
@@ -718,6 +718,26 @@ function runWithInvalidFD(func) {
printSkipMessage('Could not generate an invalid fd');
}
+// A helper function to simplify checking for ERR_INVALID_ARG_TYPE output.
+function invalidArgTypeHelper(input) {
+ if (input == null) {
+ return ` Received ${input}`;
+ }
+ if (typeof input === 'function' && input.name) {
+ return ` Received function ${input.name}`;
+ }
+ if (typeof input === 'object') {
+ if (input.constructor && input.constructor.name) {
+ return ` Received an instance of ${input.constructor.name}`;
+ }
+ return ` Received ${util.inspect(input, { depth: -1 })}`;
+ }
+ let inspected = util.inspect(input, { colors: false });
+ if (inspected.length > 25)
+ inspected = `${inspected.slice(0, 25)}...`;
+ return ` Received type ${typeof input} (${inspected})`;
+}
+
module.exports = {
allowGlobals,
buildType,
@@ -735,6 +755,7 @@ module.exports = {
hasIntl,
hasCrypto,
hasMultiLocalhost,
+ invalidArgTypeHelper,
isAIX,
isAlive,
isFreeBSD,