From b642e52130c85c2d6a159b792eb4389783aec9d1 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 This is to unsure that code using those methods won't crash if the methods are deleted in userland. --- lib/assert.js | 6 +++--- lib/events.js | 4 ++-- lib/internal/assert/assertion_error.js | 4 ++-- lib/internal/async_hooks.js | 4 ++-- lib/internal/console/constructor.js | 4 ++-- lib/internal/errors.js | 24 +++++++----------------- lib/internal/fs/utils.js | 7 +++---- lib/internal/process/warning.js | 4 ++-- 8 files changed, 23 insertions(+), 34 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 49143d64466f57..415ac30775d26a 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -22,6 +22,7 @@ const { Error, + ErrorCaptureStackTrace, ObjectAssign, ObjectIs, ObjectKeys, @@ -271,8 +272,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); @@ -791,7 +791,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 7d3ee54018d0cb..f82776072b1598 100644 --- a/lib/events.js +++ b/lib/events.js @@ -24,6 +24,7 @@ const { Boolean, Error, + ErrorCaptureStackTrace, MathMin, NumberIsNaN, ObjectCreate, @@ -291,8 +292,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..92be844eb477e1 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -3,6 +3,7 @@ const { ArrayPrototypeUnshift, Error, + ErrorCaptureStackTrace, FunctionPrototypeBind, ObjectPrototypeHasOwnProperty, ObjectDefineProperty, @@ -159,8 +160,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..824f8cd4efefa4 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -8,6 +8,7 @@ const { ArrayIsArray, Boolean, Error, + ErrorCaptureStackTrace, Map, MathFloor, Number, @@ -395,8 +396,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 2fbd5084a575d2..6f81595a742de8 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -13,6 +13,7 @@ const { ArrayIsArray, Error, + ErrorCaptureStackTrace, ErrorPrototypeToString, JSONStringify, Map, @@ -306,8 +307,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}]`; @@ -443,9 +443,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; } @@ -486,9 +484,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; } @@ -515,9 +511,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; } @@ -564,9 +558,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; } @@ -610,9 +602,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 dd2602a033f3c7..afc11fff1c9f9c 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -5,6 +5,7 @@ const { BigInt, DateNow, Error, + ErrorCaptureStackTrace, ObjectPrototypeHasOwnProperty, Number, NumberIsFinite, @@ -302,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 2822ef22f1f05a..3182988fe2ba09 100644 --- a/lib/internal/process/warning.js +++ b/lib/internal/process/warning.js @@ -3,6 +3,7 @@ const { ArrayIsArray, Error, + ErrorCaptureStackTrace, String, } = primordials; @@ -162,8 +163,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; }