preserve-caught-error
Added in v0.7.3Configuration
Rule Details
When a catch block reacts to a failure by throwing a new, more descriptive error, the original error should travel along with it as the new error's cause. This rule requires every error constructed and thrown inside a catch block to receive the caught error as its cause, so the underlying failure stays visible in stack traces and error reports.
The rule checks the global error constructors — Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, and AggregateError — and reports both a missing cause and a cause set to anything other than the caught error. Each report comes with a suggestion that attaches the caught error.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Options
requireCatchParameter
By default a catch block may omit the parameter entirely, which discards the caught error before the rule can ask for it. Set requireCatchParameter to true to require the parameter so the caught error stays available.
Examples of incorrect code for this rule with { "requireCatchParameter": true }:
Examples of correct code for this rule with { "requireCatchParameter": true }:
errorClassNames
Custom error classes are checked when their name is listed in errorClassNames. A bare string means the class takes its error options as the second argument, matching the built-in error signature. To describe a different signature, use an object with name and the 1-based argumentPosition of the options argument.
The name is matched against the constructor identifier, including the property name of a namespaced constructor such as new errors.AppError().
Examples of incorrect code for this rule with { "errorClassNames": ["AppError"] }:
Examples of correct code for this rule with { "errorClassNames": [{ "name": "AppError", "argumentPosition": 3 }] }:
Differences from ESLint
- For a constructor written without an argument list, the suggestion adds the arguments after the whole expression —
new AppError<T>becomesnew AppError<T>({ cause: error })andnew (AppError)becomesnew (AppError)({ cause: error }). ESLint puts them directly after the callee name, which lands inside the type arguments or the parentheses.