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.

PR-URL: https://github.com/nodejs/node/pull/35837
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
This commit is contained in:
Antoine du Hamel
2020-10-27 20:41:34 +01:00
committed by Node.js GitHub Bot
parent 05bb1b3f94
commit 1da672994a
8 changed files with 23 additions and 37 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -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.

View File

@@ -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);
}

View File

@@ -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);
},

View File

@@ -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;
}

View File

@@ -4,7 +4,7 @@ const {
ArrayIsArray,
BigInt,
DateNow,
Error,
ErrorCaptureStackTrace,
ObjectPrototypeHasOwnProperty,
Number,
NumberIsFinite,
@@ -302,16 +302,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;
}
}

View File

@@ -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;
}