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
Suggestion: make no-throw more restrictive #115
Comments
This seems like a nice addition, either a separate rule or an extension to no-throw. |
Fixed in #116 |
Sorry I'm a bit late to this. In the example given, the return type is marked as if (result.error !== undefined) {
handler.catch(result.error);
} else {
handler.then(result.value);
} Thus promises do not have a hidden error state using buzz()
.then(r => {
console.log(r);
})
.catch(err => {
console.error("error:", err);
}); The issue with using Hidden error state can exist in Promise but this happens with async function foo(): Promise<number> {
const x = await buzz(); // If buzz rejects, foo will reject too - i.e hidden error state exist here.
return x + 2;
}
async function buzz(): Promise<number> {
if (Math.random() > 0.5) {
return Promise.reject(new Error("bad"));
}
return 1;
} Hidden error state can be remove from this in several ways, here is one: async function foo(): Promise<number> {
const x = await buzz().catch(err => { // Hidden error state is explicitly caught making it no longer hidden.
console.error("error:", err);
return err;
});
return (
x instanceof Error
? Promise.reject(x)
: x + 2
);
}
async function buzz(): Promise<number> {
if (Math.random() > 0.5) {
return Promise.reject(new Error("bad"));
}
return 1;
} Creating a linting rule to catch hidden error state in Promises does not seem at all like an easy task. I feel this addition to the rule should be reverted. Or possibly just made a option users can manually enable. |
I think maybe it should be a separate rule. The name |
@sbdchd @RebeccaStevens Could you please check if #118 seems OK to solve this? |
LGTM |
Seems good to me. I haven't tested it but I assume you have 😜 |
There was already a test added by @sbdchd so I just moved it :-). The new no-reject rule is now released in 5.3.0 and the no-throw rule is reverted to the old behaviour. The previous version should probably have been a major release since it was not strictly additive (it added more checks to no-throw but also it altered its default behaviour). And following that, a new major version to change it back (altering the default behaviour back). However, since we are back to old behaviour I did not bother to do a major release for the latest change either. |
TL;DR: suggestion to update
no-throw
to ban usage ofPromise.reject()
Essentially this would make promises behave more like non-async code.
Currently with
no-throw
,throw
is banned, so the following errorsand if we use async await we also get an error
but we don't get an error if we explicitly use
Promise.reject()
This is a problem as it represents the same hidden error state as the previous examples, which the rule errors on.
A more functional approach would be to instead return a union or result.
Then when used, both failure and success are warned about
The text was updated successfully, but these errors were encountered: