Skip to content

Require Throw Error Capture

SuggestionAuto-fixable
small-rules/require-throw-error-capture

Require 'Error.captureStackTrace' before directly throwing new Error instances in named functions.

Diagnostic Messages

missingCaptureStackTrace
Call 'Error.captureStackTrace' on this error before throwing it so the stack trace points to the throw site.

Configuration

This rule accepts one options object after the severity.

allowOptional

Error classes that do not need Error.captureStackTrace

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/require-throw-error-capture": [
"error",
{
"allow": [
{
"name": "ValidationError"
}
]
}
]
}
}

Examples

Missing stack trace capture
function foo() {
throw new Error('bad');
}

After auto-fix

function foo() {
const error = new Error('bad');
Error.captureStackTrace(error, foo);
throw error;
}
Captured stack trace before throw
function good() {
const err = new Error('msg');
Error.captureStackTrace(err, good);
throw err;
}