Skip to content

Commit

Permalink
lib: use primordials when calling methods of Error
Browse files Browse the repository at this point in the history
This is to unsure that code using those methods won't crash if the
methods are deleted in userland.
  • Loading branch information
aduh95 committed Oct 28, 2020
1 parent 3df5afb commit b642e52
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 34 deletions.
6 changes: 3 additions & 3 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

const {
Error,
ErrorCaptureStackTrace,
ObjectAssign,
ObjectIs,
ObjectKeys,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const {
Boolean,
Error,
ErrorCaptureStackTrace,
MathMin,
NumberIsNaN,
ObjectCreate,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
Error,
ErrorCaptureStackTrace,
MathMax,
ObjectCreate,
ObjectDefineProperty,
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayPrototypeUnshift,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
ObjectPrototypeHasOwnProperty,
ObjectDefineProperty,
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
ArrayIsArray,
Boolean,
Error,
ErrorCaptureStackTrace,
Map,
MathFloor,
Number,
Expand Down Expand Up @@ -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);
},

Expand Down
24 changes: 7 additions & 17 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
const {
ArrayIsArray,
Error,
ErrorCaptureStackTrace,
ErrorPrototypeToString,
JSONStringify,
Map,
Expand Down Expand Up @@ -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}]`;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

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

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 3 additions & 4 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
BigInt,
DateNow,
Error,
ErrorCaptureStackTrace,
ObjectPrototypeHasOwnProperty,
Number,
NumberIsFinite,
Expand Down Expand Up @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayIsArray,
Error,
ErrorCaptureStackTrace,
String,
} = primordials;

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit b642e52

Please sign in to comment.