From 41d997cc720170f636c4963b35658358139d0820 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 27 Oct 2020 20:41:34 +0100 Subject: [PATCH] lib: use primordials when calling methods of Error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is to unsure that code using those methods won't crash if the methods are deleted in userland. PR-URL: https://github.com/nodejs/node/pull/35837 Reviewed-By: Michaƫl Zasso Reviewed-By: Rich Trott Reviewed-By: Shingo Inoue --- lib/assert.js | 6 +++--- lib/events.js | 4 ++-- lib/internal/assert/assertion_error.js | 4 ++-- lib/internal/async_hooks.js | 5 ++--- lib/internal/console/constructor.js | 5 ++--- lib/internal/errors.js | 24 +++++++----------------- lib/internal/fs/utils.js | 8 +++----- lib/internal/process/warning.js | 4 ++-- 8 files changed, 23 insertions(+), 37 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index e6f401d5507c3d..11a2cc9a3328e7 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -22,6 +22,7 @@ const { Error, + ErrorCaptureStackTrace, ObjectAssign, ObjectIs, ObjectKeys, @@ -269,8 +270,7 @@ function getErrMessage(message, fn) { // We only need the stack trace. To minimize the overhead use an object // instead of an error. const err = {}; - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(err, fn); + ErrorCaptureStackTrace(err, fn); Error.stackTraceLimit = tmpLimit; overrideStackTrace.set(err, (_, stack) => stack); @@ -789,7 +789,7 @@ function hasMatchingError(actual, expected) { if (expected.prototype !== undefined && actual instanceof expected) { return true; } - if (Error.isPrototypeOf(expected)) { + if (ObjectPrototypeIsPrototypeOf(Error, expected)) { return false; } return expected.call({}, actual) === true; diff --git a/lib/events.js b/lib/events.js index 8570c0a8fbaea3..48da5e0ed1b72d 100644 --- a/lib/events.js +++ b/lib/events.js @@ -24,6 +24,7 @@ const { Boolean, Error, + ErrorCaptureStackTrace, MathMin, NumberIsNaN, ObjectCreate, @@ -279,8 +280,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) { if (er instanceof Error) { try { const capture = {}; - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(capture, EventEmitter.prototype.emit); + ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit); ObjectDefineProperty(er, kEnhanceStackBeforeInspector, { value: enhanceStackTrace.bind(this, er, capture), configurable: true diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index c89219eef82224..ff2d2dbc617557 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -2,6 +2,7 @@ const { Error, + ErrorCaptureStackTrace, MathMax, ObjectCreate, ObjectDefineProperty, @@ -444,8 +445,7 @@ class AssertionError extends Error { this.expected = expected; this.operator = operator; } - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(this, stackStartFn || stackStartFunction); + ErrorCaptureStackTrace(this, stackStartFn || stackStartFunction); // Create error message including the error code in the name. this.stack; // Reset the name. diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 9463d1d3348e67..adc87f9ed9662d 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -2,7 +2,7 @@ const { ArrayPrototypeUnshift, - Error, + ErrorCaptureStackTrace, FunctionPrototypeBind, ObjectPrototypeHasOwnProperty, ObjectDefineProperty, @@ -159,8 +159,7 @@ function fatalError(e) { process._rawDebug(e.stack); } else { const o = { message: e }; - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(o, fatalError); + ErrorCaptureStackTrace(o, fatalError); process._rawDebug(o.stack); } diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index aae511cb6c7ca0..8684bea5ad4966 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -7,7 +7,7 @@ const { ArrayFrom, ArrayIsArray, Boolean, - Error, + ErrorCaptureStackTrace, Map, MathFloor, Number, @@ -395,8 +395,7 @@ const consoleMethods = { name: 'Trace', message: this[kFormatForStderr](args) }; - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(err, trace); + ErrorCaptureStackTrace(err, trace); this.error(err.stack); }, diff --git a/lib/internal/errors.js b/lib/internal/errors.js index c2b853a5ff0b9a..ea3e0cc19eade7 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -13,6 +13,7 @@ const { ArrayIsArray, Error, + ErrorCaptureStackTrace, ErrorPrototypeToString, JSONStringify, Map, @@ -302,8 +303,7 @@ function hideStackFrames(fn) { function addCodeToName(err, name, code) { // Set the stack if (excludedStackFn !== undefined) { - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(err, excludedStackFn); + ErrorCaptureStackTrace(err, excludedStackFn); } // Add the error code to the name to include it in the stack trace. err.name = `${name} [${code}]`; @@ -439,9 +439,7 @@ function uvException(ctx) { if (dest) { err.dest = dest; } - - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(err, excludedStackFn || uvException); + ErrorCaptureStackTrace(err, excludedStackFn || uvException); return err; } @@ -482,9 +480,7 @@ function uvExceptionWithHostPort(err, syscall, address, port) { if (port) { ex.port = port; } - - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(ex, excludedStackFn || uvExceptionWithHostPort); + ErrorCaptureStackTrace(ex, excludedStackFn || uvExceptionWithHostPort); return ex; } @@ -511,9 +507,7 @@ function errnoException(err, syscall, original) { ex.errno = err; ex.code = code; ex.syscall = syscall; - - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(ex, excludedStackFn || errnoException); + ErrorCaptureStackTrace(ex, excludedStackFn || errnoException); return ex; } @@ -560,9 +554,7 @@ function exceptionWithHostPort(err, syscall, address, port, additional) { if (port) { ex.port = port; } - - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(ex, excludedStackFn || exceptionWithHostPort); + ErrorCaptureStackTrace(ex, excludedStackFn || exceptionWithHostPort); return ex; } @@ -606,9 +598,7 @@ function dnsException(code, syscall, hostname) { if (hostname) { ex.hostname = hostname; } - - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(ex, excludedStackFn || dnsException); + ErrorCaptureStackTrace(ex, excludedStackFn || dnsException); return ex; } diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index f14e34e7c62e06..e3255cdb2f2562 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -4,7 +4,7 @@ const { ArrayIsArray, BigInt, DateNow, - Error, + ErrorCaptureStackTrace, ObjectPrototypeHasOwnProperty, Number, NumberIsFinite, @@ -303,16 +303,14 @@ function getOptions(options, defaultOptions) { function handleErrorFromBinding(ctx) { if (ctx.errno !== undefined) { // libuv error numbers const err = uvException(ctx); - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(err, handleErrorFromBinding); + ErrorCaptureStackTrace(err, handleErrorFromBinding); throw err; } if (ctx.error !== undefined) { // Errors created in C++ land. // TODO(joyeecheung): currently, ctx.error are encoding errors // usually caused by memory problems. We need to figure out proper error // code(s) for this. - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(ctx.error, handleErrorFromBinding); + ErrorCaptureStackTrace(ctx.error, handleErrorFromBinding); throw ctx.error; } } diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js index f10e07f6210c30..ffea12cd81d04c 100644 --- a/lib/internal/process/warning.js +++ b/lib/internal/process/warning.js @@ -3,6 +3,7 @@ const { ArrayIsArray, Error, + ErrorCaptureStackTrace, String, } = primordials; @@ -157,8 +158,7 @@ function createWarningObject(warning, type, code, ctor, detail) { warning.name = String(type || 'Warning'); if (code !== undefined) warning.code = code; if (detail !== undefined) warning.detail = detail; - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(warning, ctor || process.emitWarning); + ErrorCaptureStackTrace(warning, ctor || process.emitWarning); return warning; }