aboutsummaryrefslogtreecommitdiff
path: root/test/parallel
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-11-18 04:19:16 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2018-12-03 12:37:34 +0100
commit2b5f2bc68b67621b42f6a698792532d1149e6196 (patch)
tree02169fc14e15f291626f9915e991ea1506018860 /test/parallel
parent1fe824bcbb0267265a8ccdb7301b7b8e3e8779f3 (diff)
downloadandroid-node-v8-2b5f2bc68b67621b42f6a698792532d1149e6196.tar.gz
android-node-v8-2b5f2bc68b67621b42f6a698792532d1149e6196.tar.bz2
android-node-v8-2b5f2bc68b67621b42f6a698792532d1149e6196.zip
util: improve internal `isError()` validation
The current internal isError function checked the toString value instead of using the more precise `util.types.isNativeError()` check. The `instanceof` check is not removed due to possible errors that are not native but still an instance of Error. PR-URL: https://github.com/nodejs/node/pull/24746 Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'test/parallel')
-rw-r--r--test/parallel/test-internal-util-helpers.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/test/parallel/test-internal-util-helpers.js b/test/parallel/test-internal-util-helpers.js
new file mode 100644
index 0000000000..bf60cff9bd
--- /dev/null
+++ b/test/parallel/test-internal-util-helpers.js
@@ -0,0 +1,37 @@
+// Flags: --expose-internals
+'use strict';
+
+require('../common');
+const assert = require('assert');
+const { types } = require('util');
+const { isError } = require('internal/util');
+const vm = require('vm');
+
+// Special cased errors. Test the internal function which is used in
+// `util.inspect()`, the `repl` and maybe more. This verifies that errors from
+// different realms, and non native instances of error are properly detected as
+// error while definitely false ones are not detected. This is different than
+// the public `util.isError()` function which falsy detects the fake errors as
+// actual errors.
+{
+ const fake = { [Symbol.toStringTag]: 'Error' };
+ assert(!types.isNativeError(fake));
+ assert(!(fake instanceof Error));
+ assert(!isError(fake));
+
+ const err = new Error('test');
+ const newErr = Object.create(
+ Object.getPrototypeOf(err),
+ Object.getOwnPropertyDescriptors(err));
+ Object.defineProperty(err, 'message', { value: err.message });
+ assert(types.isNativeError(err));
+ assert(!types.isNativeError(newErr));
+ assert(newErr instanceof Error);
+ assert(isError(newErr));
+
+ const context = vm.createContext({});
+ const differentRealmErr = vm.runInContext('new Error()', context);
+ assert(types.isNativeError(differentRealmErr));
+ assert(!(differentRealmErr instanceof Error));
+ assert(isError(differentRealmErr));
+}