Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: no-identical-catch #2046

Open
dimaMachina opened this issue Feb 24, 2023 · 4 comments
Open

Rule proposal: no-identical-catch #2046

dimaMachina opened this issue Feb 24, 2023 · 4 comments

Comments

@dimaMachina
Copy link
Contributor

dimaMachina commented Feb 24, 2023

Description

Same catch statement inside another catch statement can be safely removed

Fail

try {
  if (foo) {
    try {
      for await (const result of value) {
        handleResponse(result);
      }
      setIsFetching(false);
      setSubscription(null);
    } catch (error) {
      setIsFetching(false); // ❌ duplicates can be safely removed
      setResponse(formatError(error)); // ❌ duplicates can be safely removed
      setSubscription(null); // ❌ duplicates can be safely removed
    }
  } else {
    handleResponse(value);
  }
} catch (error) {
  setIsFetching(false);
  setResponse(formatError(error));
  setSubscription(null);
}

Pass

try {
  if (foo) {
    try {
      for await (const result of value) {
        handleResponse(result);
      }
      setIsFetching(false);
      setSubscription(null);
    } catch (error) {
      setIsFetching(false);
      setResponse(someAnotherFunction(error));
      setSubscription(null);
    }
  } else {
    handleResponse(value);
  }
} catch (error) {
  setIsFetching(false);
  setResponse(formatError(error));
  setSubscription(null);
}

Additional Info

real-world example graphql/graphiql#3033

@fisker
Copy link
Collaborator

fisker commented Feb 24, 2023

What if setResponse is like

function setResponse(error) {
  if (error instanceof FooError) {
     throw new BarError()
  }

  if (error instanceof BarError) {
     // ...
  }
}

@dimaMachina
Copy link
Contributor Author

@fisker possible but very rare case, a perfect case for eslint-disable comment

@fisker
Copy link
Collaborator

fisker commented Feb 24, 2023

I'd say this example is also rare case ...

Anyway, if this get accepted, as a reminder, should be carefully when there are flow control statements, including return/break/continue/throw.

@fisker
Copy link
Collaborator

fisker commented Feb 24, 2023

Another possible rule proposal in this code.

    try {
+    try {
      // ...
+    } finally {
      setIsFetching(false);
+    }      
-      setSubscription(null);
    } catch (error) {
-      setIsFetching(false);
      setResponse(someAnotherFunction(error));
-      setSubscription(null);
    }
+   finally {
+      setSubscription(null);
+   }

(typing on cellphone, maybe bad indentation)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants