Skip to content

errorUnnecessaryCaptureStackTraces

Reports unnecessary Error.captureStackTrace() calls in Error subclass constructors.

✅ This rule is included in the ts logical and logicalStrict presets.

Calling Error.captureStackTrace() inside the constructor of a built-in Error subclass is unnecessary, since the Error constructor calls it automatically.

class
class MyError
MyError
extends
var Error: ErrorConstructor
Error
{
constructor() {
var Error: ErrorConstructor
Error
.
ErrorConstructor.captureStackTrace(targetObject: object, constructorOpt?: Function): void

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();

captureStackTrace
(this,
class MyError
MyError
);
}
}
class
class MyError
MyError
extends
var Error: ErrorConstructor
Error
{
constructor() {
var Error: ErrorConstructor
Error
.
ErrorConstructor.captureStackTrace(targetObject: object, constructorOpt?: Function): void

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();

captureStackTrace
?.(this,
class MyError
MyError
);
}
}
class
class MyError
MyError
extends
var Error: ErrorConstructor
Error
{
constructor() {
var Error: ErrorConstructor
Error
.
ErrorConstructor.captureStackTrace(targetObject: object, constructorOpt?: Function): void

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();

captureStackTrace
(this, this.
Object.constructor: Function

The initial value of Object.prototype.constructor is the standard built-in Object constructor.

constructor
);
}
}
class
class MyError
MyError
extends
var Error: ErrorConstructor
Error
{
constructor() {
var Error: ErrorConstructor
Error
.
ErrorConstructor.captureStackTrace(targetObject: object, constructorOpt?: Function): void

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();

captureStackTrace
(this, new.
class MyError
target
);
}
}

This rule is not configurable.

If you’re extending a custom error class that changes the default behavior to not capture the stack trace, you may need to disable this rule for that specific class.

Made with ❤️‍🔥 around the world by the Flint team and contributors.