-
Notifications
You must be signed in to change notification settings - Fork 48
Require an Error object when rejecting a promise #81
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
Require an Error object when rejecting a promise #81
Conversation
|
How does this rule deal with libraries that argument error objects like |
|
I agree that |
|
@3rd-Eden custom error types should work just fine. https://eslint.org/docs/rules/prefer-promise-reject-errors This also confirms what I'm saying: https://eslint.org/docs/rules/prefer-promise-reject-errors#known-limitations |
|
Did you actually test these rules or did you make the assumption that they work in the way you wanted them to work? I took some time today to specify these rules in an example project to ensure that they would not trigger on valid cases such as custom errors and enhanced error objects. But during this testing I noticed that these rules are only triggered when you as developer intentionally types throw null;
throw undefined When you, as developer explicitly type of those values I would assume that there is a valid reason for it, and it should be allowed. The things that you do care about it when you accidentally throw a This however does not trigger the lint rule: const x;
throw x;
new Promise(function (resolve, reject) {
reject(x);
})The examples above are things you can to get caught, but are ignored by your proposed rules. Instead if only triggers on examples where developers explicitly set So given this in-depth analysis for the actual rule and how it works with actual code I have give a -1 on this addition. |
|
I'd offer that omitting something from throw/reject or throwing a non-Error is typically the wrong thing to do. If there was a reason a developer decides to do it anyway, they can always disable the rule on that line. Throwing things like strings as errors cause complications/bugs for things like global error handling, so this seems like a good use of a linter (flagging probable bugs). Of course type constraints are better, but because so many JS developers shy away from using types, it's useful to have linting rules as a fallback. |
Yes, we are using this on our project.
The
Thank you for your input. So to summarize, these rules are mostly intended to protect against the 2 following sort of common and mostly accidental cases: throw 'this error';
// and
Promise.reject(); |
|
Regarding https://eslint.org/docs/rules/no-throw-literal, the purpose of these eslint rules is to define standards and although there might be edge cases where someone would want to Regarding https://eslint.org/docs/rules/prefer-promise-reject-errors, I see it as a good standard to provide an error object over any other format, however I would suggest we relax the setting to |
| 'strict': [2, 'global'], | ||
| 'use-isnan': 2, | ||
| 'valid-jsdoc': [2, { prefer: { return: 'returns' }, requireReturn: false }], | ||
| 'prefer-promise-reject-errors': 2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest relaxing this new rule by adding allowEmptyReject: true to allow Promise.reject() to still work, but Promise.reject("no") to error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, after thinking about it a bit more, disregard. the following code would still produce a catch w/ undefined which I would not want. so even Promise.reject should always be called with an error object.
async function example() {
return Promise.reject();
}
(async () => {
try {
const res = await example();
} catch (e) {
console.log('err', e); // undefined
}
})();
indexzero
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great discussion 🤟
* Require an Error object when rejecting a promise * reverting lock changes
Rejecting without an
Erroris a really bad pattern that can be easy to miss.catchcode should not have to expect anundefinedornullerror.https://eslint.org/docs/rules/prefer-promise-reject-errors
I also went ahead and made
no-throw-literalan error. throwing a string literal, or anything that is not anErrormakes debugging a horrible experience, because there is nostack.So these 2 are very related and should not only be discouraged, but considered errors.
https://eslint.org/docs/rules/no-throw-literal