Skip to content

Commit 41d997c

Browse files
aduh95BethGriggs
authored andcommittedDec 15, 2020
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 3f3d2d7 commit 41d997c

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,
@@ -269,8 +270,7 @@ function getErrMessage(message, fn) {
269270
// We only need the stack trace. To minimize the overhead use an object
270271
// instead of an error.
271272
const err = {};
272-
// eslint-disable-next-line no-restricted-syntax
273-
Error.captureStackTrace(err, fn);
273+
ErrorCaptureStackTrace(err, fn);
274274
Error.stackTraceLimit = tmpLimit;
275275

276276
overrideStackTrace.set(err, (_, stack) => stack);
@@ -789,7 +789,7 @@ function hasMatchingError(actual, expected) {
789789
if (expected.prototype !== undefined && actual instanceof expected) {
790790
return true;
791791
}
792-
if (Error.isPrototypeOf(expected)) {
792+
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
793793
return false;
794794
}
795795
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,
@@ -279,8 +280,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
279280
if (er instanceof Error) {
280281
try {
281282
const capture = {};
282-
// eslint-disable-next-line no-restricted-syntax
283-
Error.captureStackTrace(capture, EventEmitter.prototype.emit);
283+
ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
284284
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
285285
value: enhanceStackTrace.bind(this, er, capture),
286286
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,
@@ -302,8 +303,7 @@ function hideStackFrames(fn) {
302303
function addCodeToName(err, name, code) {
303304
// Set the stack
304305
if (excludedStackFn !== undefined) {
305-
// eslint-disable-next-line no-restricted-syntax
306-
Error.captureStackTrace(err, excludedStackFn);
306+
ErrorCaptureStackTrace(err, excludedStackFn);
307307
}
308308
// Add the error code to the name to include it in the stack trace.
309309
err.name = `${name} [${code}]`;
@@ -439,9 +439,7 @@ function uvException(ctx) {
439439
if (dest) {
440440
err.dest = dest;
441441
}
442-
443-
// eslint-disable-next-line no-restricted-syntax
444-
Error.captureStackTrace(err, excludedStackFn || uvException);
442+
ErrorCaptureStackTrace(err, excludedStackFn || uvException);
445443
return err;
446444
}
447445

@@ -482,9 +480,7 @@ function uvExceptionWithHostPort(err, syscall, address, port) {
482480
if (port) {
483481
ex.port = port;
484482
}
485-
486-
// eslint-disable-next-line no-restricted-syntax
487-
Error.captureStackTrace(ex, excludedStackFn || uvExceptionWithHostPort);
483+
ErrorCaptureStackTrace(ex, excludedStackFn || uvExceptionWithHostPort);
488484
return ex;
489485
}
490486

@@ -511,9 +507,7 @@ function errnoException(err, syscall, original) {
511507
ex.errno = err;
512508
ex.code = code;
513509
ex.syscall = syscall;
514-
515-
// eslint-disable-next-line no-restricted-syntax
516-
Error.captureStackTrace(ex, excludedStackFn || errnoException);
510+
ErrorCaptureStackTrace(ex, excludedStackFn || errnoException);
517511
return ex;
518512
}
519513

@@ -560,9 +554,7 @@ function exceptionWithHostPort(err, syscall, address, port, additional) {
560554
if (port) {
561555
ex.port = port;
562556
}
563-
564-
// eslint-disable-next-line no-restricted-syntax
565-
Error.captureStackTrace(ex, excludedStackFn || exceptionWithHostPort);
557+
ErrorCaptureStackTrace(ex, excludedStackFn || exceptionWithHostPort);
566558
return ex;
567559
}
568560

@@ -606,9 +598,7 @@ function dnsException(code, syscall, hostname) {
606598
if (hostname) {
607599
ex.hostname = hostname;
608600
}
609-
610-
// eslint-disable-next-line no-restricted-syntax
611-
Error.captureStackTrace(ex, excludedStackFn || dnsException);
601+
ErrorCaptureStackTrace(ex, excludedStackFn || dnsException);
612602
return ex;
613603
}
614604

‎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,
@@ -303,16 +303,14 @@ function getOptions(options, defaultOptions) {
303303
function handleErrorFromBinding(ctx) {
304304
if (ctx.errno !== undefined) { // libuv error numbers
305305
const err = uvException(ctx);
306-
// eslint-disable-next-line no-restricted-syntax
307-
Error.captureStackTrace(err, handleErrorFromBinding);
306+
ErrorCaptureStackTrace(err, handleErrorFromBinding);
308307
throw err;
309308
}
310309
if (ctx.error !== undefined) { // Errors created in C++ land.
311310
// TODO(joyeecheung): currently, ctx.error are encoding errors
312311
// usually caused by memory problems. We need to figure out proper error
313312
// code(s) for this.
314-
// eslint-disable-next-line no-restricted-syntax
315-
Error.captureStackTrace(ctx.error, handleErrorFromBinding);
313+
ErrorCaptureStackTrace(ctx.error, handleErrorFromBinding);
316314
throw ctx.error;
317315
}
318316
}

‎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

@@ -157,8 +158,7 @@ function createWarningObject(warning, type, code, ctor, detail) {
157158
warning.name = String(type || 'Warning');
158159
if (code !== undefined) warning.code = code;
159160
if (detail !== undefined) warning.detail = detail;
160-
// eslint-disable-next-line no-restricted-syntax
161-
Error.captureStackTrace(warning, ctor || process.emitWarning);
161+
ErrorCaptureStackTrace(warning, ctor || process.emitWarning);
162162
return warning;
163163
}
164164

0 commit comments

Comments
 (0)
Please sign in to comment.