Skip to content

New Rule: preserve-caught-error #19844

@Amnish04

Description

@Amnish04

Rule details

Report any instances of swallowed error cause when re-throwing exceptions.

Related ECMAScript feature

Error: cause (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)

What type of rule is this?

Warns about a potential problem

Example code

try {
    // This might throw an error with important details of failure
    fetch("some resource");
} catch (error) {
    throw new Error("Failed to perform some high level operation", {
        // Not using this would mean losing important error context
        // that might have been valuable during debugging.
        // This is what the rule flags
        cause: error,
    });
}

Why should this rule be in the core instead of a plugin?

This rule aligns well with core ESlint guidelines regarding new rules:

1. Widely applicable: This rule aims to enforce proper usage of the error cause property that was introduced to solve the problem of lost error context when re-throwing more specific exceptions. Robust error handling is a crucial part of every serious project regardless of the library/framework being used, making this a widely applicable and fairly generic rule.
2. Community Acceptance: I created an ESLint plugin for this initially as we needed this at work, but I received only positive feedback regarding how badly people needed this on various publicizing posts I made.
3. ECMAScript Feature: The rule is related to a stage 4 ECMAScript feature.

I originally started a discussion and @nzakas suggests this could potentially be a good fit in the set of core rules.

Participation

  • I am willing to submit a pull request to implement this rule.

Additional comments

No response

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions