-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
catch must re-throw #7
Comments
Hey @xjamundx I was just going to suggest this till I saw this issue! :) I have implemented this via ASTExplorer here! Happy to make a pull request if you would like. Background: We have found that is definitely the exception in our codebase where you would actually want to opt-out of re-throwing/explicitly handling your errors and our developers should opt-in (via eslint-disable-line) if they really want this behaviour (which is rarely ever the case). Accidental promise swallowing is a real pain! :) |
Sorry I'm finally getting back to this after like 6 months. I'm going to poke around your astexplorer code. Still seems like a good idea! |
I'm willing to implement this. I was wondering if it could be included in In my implementation, there would be 3 possible failure messages from 'Each then() should return a value or throw' //the existing one
'Each then() error handler should return a value or rethrow' //2nd argument in .then(..)
'Each catch() should return a value or throw' //.catch |
… handlers as well.
I redid my implementation with the refactored always-return. You can have a look on my fork. No doc updates yet. I'll wait to hear back on what you think of this being included in I can put a config option for it too, but I think it should be enabled by default. I find missing a return or rethrow in a |
THanks for doing this. Will check it out tomorrow! |
@r-murphy A catch should either resolve (to handle recovered errors and follow with the success path) or reject to follow the rejection path for unrecoverable errors. So the rule should enforce either a resolve or a reject in the catch, but must error if the catch does not return at all. Such errors are very hard to find and track in the code because, if missing, it just resolves implicitly with undefined. |
@HyperBrain Yes, I agree. And that's why my change was checking for. Are you seeing different behaviour? |
At least in the unit tests I'm missing a check that tests for an error like this one: They should trigger an error, shouldn't they? |
@HyperBrain Those error cases are covered in my unit tests. Here's the commit diff. My commit was from 2016 so it probably won't even merge in anymore anyways. |
@r-murphy @HyperBrain thanks for reviving this discussion and linking to a patch. I would like to do some more reading on this to gain a better understanding. I also wonder where #12 fits into this issue, specifically: // propagating the error is the default error handler for a promise, it can be omitted
something.then(null, function (err) {
throw err;
});
// or
something.catch(function (err) {
throw err;
}); I'm wondering if this should be a part of |
@macklinu I think to make it sound, there need be separate rules. An interesting one would also be to restrict the promise error handler pattern itself - e.g. companies would likely restrict their devs to use only catch, but no then with an error handler parameter. So for that a rule like The rule extension discussed here would also fit into it's own rule (instead of |
This is such an essential rule. Catch all blocks have already caused me so much pain. Any chance this will land soon? |
I am building a little eslint plugin that warns about catch blocks that don't rethrow, this only supports async-await style code though, no |
I've heard that
catch()
should re-throw the error orreturn Promise.reject(err)
anyway familiar with this? It's so you don't break the promise chain.The text was updated successfully, but these errors were encountered: