summaryrefslogtreecommitdiff
path: root/lib
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 /lib
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 'lib')
-rw-r--r--lib/internal/util.js8
-rw-r--r--lib/util.js12
2 files changed, 17 insertions, 3 deletions
diff --git a/lib/internal/util.js b/lib/internal/util.js
index 414d037a29..ee550fad2e 100644
--- a/lib/internal/util.js
+++ b/lib/internal/util.js
@@ -12,6 +12,9 @@ const {
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
decorated_private_symbol: kDecoratedPrivateSymbolIndex
} = internalBinding('util');
+const {
+ isNativeError
+} = internalBinding('types');
const { errmap } = internalBinding('uv');
@@ -26,7 +29,10 @@ function removeColors(str) {
}
function isError(e) {
- return objectToString(e) === '[object Error]' || e instanceof Error;
+ // An error could be an instance of Error while not being a native error
+ // or could be from a different realm and not be instance of Error but still
+ // be a native error.
+ return isNativeError(e) || e instanceof Error;
}
function objectToString(o) {
diff --git a/lib/util.js b/lib/util.js
index 63b34f48bf..8f7f3a92dd 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -42,10 +42,16 @@ const {
const {
deprecate,
getSystemErrorName: internalErrorName,
- isError,
promisify,
} = require('internal/util');
+const ReflectApply = Reflect.apply;
+
+function uncurryThis(func) {
+ return (thisArg, ...args) => ReflectApply(func, thisArg, args);
+}
+const objectToString = uncurryThis(Object.prototype.toString);
+
let CIRCULAR_ERROR_MESSAGE;
let internalDeepEqual;
@@ -443,7 +449,9 @@ module.exports = exports = {
isRegExp,
isObject,
isDate,
- isError,
+ isError(e) {
+ return objectToString(e) === '[object Error]' || e instanceof Error;
+ },
isFunction,
isPrimitive,
log,