Skip to content

eslint/preserve-caught-error Suspicious

🚧 An auto-fix is still under development.

What it does

Enforces that when re-throwing an error in a catch block, the original error is preserved using the 'cause' property.

Why is this bad?

Re-throwing an error without preserving the original error loses important debugging information and makes it harder to trace the root cause of issues.

Examples

Examples of incorrect code for this rule:

js
try {
  doSomething();
} catch (err) {
  throw new Error("Something failed");
}

Examples of correct code for this rule:

js
try {
  doSomething();
} catch (err) {
  throw new Error("Something failed", { cause: err });
}

Configuration

This rule accepts a configuration object with the following properties:

requireCatchParameter

type: boolean

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny preserve-caught-error
json
{
  "rules": {
    "preserve-caught-error": "error"
  }
}

References

Released under the MIT License.