Skip to content

Commit 1da6729

Browse files
aduh95nodejs-github-bot
authored andcommitted
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: #35837 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
1 parent 05bb1b3 commit 1da6729

File tree

8 files changed

+23
-37
lines changed

8 files changed

+23
-37
lines changed

lib/assert.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
const {
2424
Error,
25+
ErrorCaptureStackTrace,
2526
ObjectAssign,
2627
ObjectIs,
2728
ObjectKeys,
@@ -271,8 +272,7 @@ function getErrMessage(message, fn) {
271272
// We only need the stack trace. To minimize the overhead use an object
272273
// instead of an error.
273274
const err = {};
274-
// eslint-disable-next-line no-restricted-syntax
275-
Error.captureStackTrace(err, fn);
275+
ErrorCaptureStackTrace(err, fn);
276276
Error.stackTraceLimit = tmpLimit;
277277

278278
overrideStackTrace.set(err, (_, stack) => stack);
@@ -791,7 +791,7 @@ function hasMatchingError(actual, expected) {
791791
if (expected.prototype !== undefined && actual instanceof expected) {
792792
return true;
793793
}
794-
if (Error.isPrototypeOf(expected)) {
794+
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
795795
return false;
796796
}
797797
return expected.call({}, actual) === true;

lib/events.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const {
2525
Boolean,
2626
Error,
27+
ErrorCaptureStackTrace,
2728
MathMin,
2829
NumberIsNaN,
2930
ObjectCreate,
@@ -291,8 +292,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
291292
if (er instanceof Error) {
292293
try {
293294
const capture = {};
294-
// eslint-disable-next-line no-restricted-syntax
295-
Error.captureStackTrace(capture, EventEmitter.prototype.emit);
295+
ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
296296
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
297297
value: enhanceStackTrace.bind(this, er, capture),
298298
configurable: true

lib/internal/assert/assertion_error.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
Error,
5+
ErrorCaptureStackTrace,
56
MathMax,
67
ObjectCreate,
78
ObjectDefineProperty,
@@ -444,8 +445,7 @@ class AssertionError extends Error {
444445
this.expected = expected;
445446
this.operator = operator;
446447
}
447-
// eslint-disable-next-line no-restricted-syntax
448-
Error.captureStackTrace(this, stackStartFn || stackStartFunction);
448+
ErrorCaptureStackTrace(this, stackStartFn || stackStartFunction);
449449
// Create error message including the error code in the name.
450450
this.stack;
451451
// Reset the name.

lib/internal/async_hooks.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const {
44
ArrayPrototypeUnshift,
5-
Error,
5+
ErrorCaptureStackTrace,
66
FunctionPrototypeBind,
77
ObjectPrototypeHasOwnProperty,
88
ObjectDefineProperty,
@@ -159,8 +159,7 @@ function fatalError(e) {
159159
process._rawDebug(e.stack);
160160
} else {
161161
const o = { message: e };
162-
// eslint-disable-next-line no-restricted-syntax
163-
Error.captureStackTrace(o, fatalError);
162+
ErrorCaptureStackTrace(o, fatalError);
164163
process._rawDebug(o.stack);
165164
}
166165

lib/internal/console/constructor.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
ArrayFrom,
88
ArrayIsArray,
99
Boolean,
10-
Error,
10+
ErrorCaptureStackTrace,
1111
Map,
1212
MathFloor,
1313
Number,
@@ -395,8 +395,7 @@ const consoleMethods = {
395395
name: 'Trace',
396396
message: this[kFormatForStderr](args)
397397
};
398-
// eslint-disable-next-line no-restricted-syntax
399-
Error.captureStackTrace(err, trace);
398+
ErrorCaptureStackTrace(err, trace);
400399
this.error(err.stack);
401400
},
402401

lib/internal/errors.js

+7-17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
const {
1414
ArrayIsArray,
1515
Error,
16+
ErrorCaptureStackTrace,
1617
ErrorPrototypeToString,
1718
JSONStringify,
1819
Map,
@@ -306,8 +307,7 @@ function hideStackFrames(fn) {
306307
function addCodeToName(err, name, code) {
307308
// Set the stack
308309
if (excludedStackFn !== undefined) {
309-
// eslint-disable-next-line no-restricted-syntax
310-
Error.captureStackTrace(err, excludedStackFn);
310+
ErrorCaptureStackTrace(err, excludedStackFn);
311311
}
312312
// Add the error code to the name to include it in the stack trace.
313313
err.name = `${name} [${code}]`;
@@ -443,9 +443,7 @@ function uvException(ctx) {
443443
if (dest) {
444444
err.dest = dest;
445445
}
446-
447-
// eslint-disable-next-line no-restricted-syntax
448-
Error.captureStackTrace(err, excludedStackFn || uvException);
446+
ErrorCaptureStackTrace(err, excludedStackFn || uvException);
449447
return err;
450448
}
451449

@@ -486,9 +484,7 @@ function uvExceptionWithHostPort(err, syscall, address, port) {
486484
if (port) {
487485
ex.port = port;
488486
}
489-
490-
// eslint-disable-next-line no-restricted-syntax
491-
Error.captureStackTrace(ex, excludedStackFn || uvExceptionWithHostPort);
487+
ErrorCaptureStackTrace(ex, excludedStackFn || uvExceptionWithHostPort);
492488
return ex;
493489
}
494490

@@ -515,9 +511,7 @@ function errnoException(err, syscall, original) {
515511
ex.errno = err;
516512
ex.code = code;
517513
ex.syscall = syscall;
518-
519-
// eslint-disable-next-line no-restricted-syntax
520-
Error.captureStackTrace(ex, excludedStackFn || errnoException);
514+
ErrorCaptureStackTrace(ex, excludedStackFn || errnoException);
521515
return ex;
522516
}
523517

@@ -564,9 +558,7 @@ function exceptionWithHostPort(err, syscall, address, port, additional) {
564558
if (port) {
565559
ex.port = port;
566560
}
567-
568-
// eslint-disable-next-line no-restricted-syntax
569-
Error.captureStackTrace(ex, excludedStackFn || exceptionWithHostPort);
561+
ErrorCaptureStackTrace(ex, excludedStackFn || exceptionWithHostPort);
570562
return ex;
571563
}
572564

@@ -610,9 +602,7 @@ function dnsException(code, syscall, hostname) {
610602
if (hostname) {
611603
ex.hostname = hostname;
612604
}
613-
614-
// eslint-disable-next-line no-restricted-syntax
615-
Error.captureStackTrace(ex, excludedStackFn || dnsException);
605+
ErrorCaptureStackTrace(ex, excludedStackFn || dnsException);
616606
return ex;
617607
}
618608

lib/internal/fs/utils.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
ArrayIsArray,
55
BigInt,
66
DateNow,
7-
Error,
7+
ErrorCaptureStackTrace,
88
ObjectPrototypeHasOwnProperty,
99
Number,
1010
NumberIsFinite,
@@ -302,16 +302,14 @@ function getOptions(options, defaultOptions) {
302302
function handleErrorFromBinding(ctx) {
303303
if (ctx.errno !== undefined) { // libuv error numbers
304304
const err = uvException(ctx);
305-
// eslint-disable-next-line no-restricted-syntax
306-
Error.captureStackTrace(err, handleErrorFromBinding);
305+
ErrorCaptureStackTrace(err, handleErrorFromBinding);
307306
throw err;
308307
}
309308
if (ctx.error !== undefined) { // Errors created in C++ land.
310309
// TODO(joyeecheung): currently, ctx.error are encoding errors
311310
// usually caused by memory problems. We need to figure out proper error
312311
// code(s) for this.
313-
// eslint-disable-next-line no-restricted-syntax
314-
Error.captureStackTrace(ctx.error, handleErrorFromBinding);
312+
ErrorCaptureStackTrace(ctx.error, handleErrorFromBinding);
315313
throw ctx.error;
316314
}
317315
}

lib/internal/process/warning.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayIsArray,
55
Error,
6+
ErrorCaptureStackTrace,
67
String,
78
} = primordials;
89

@@ -162,8 +163,7 @@ function createWarningObject(warning, type, code, ctor, detail) {
162163
warning.name = String(type || 'Warning');
163164
if (code !== undefined) warning.code = code;
164165
if (detail !== undefined) warning.detail = detail;
165-
// eslint-disable-next-line no-restricted-syntax
166-
Error.captureStackTrace(warning, ctor || process.emitWarning);
166+
ErrorCaptureStackTrace(warning, ctor || process.emitWarning);
167167
return warning;
168168
}
169169

0 commit comments

Comments
 (0)