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
throwing non-errors (specifically strings) #1791
Comments
We do not. For future reference, our rules are all documented: http://eslint.org/docs/rules/ |
Well this is a feature request then :) |
Please take a moment to read over what new rule proposals should include: http://eslint.org/docs/developer-guide/contributing.html#new-rules |
I would very much like to see this as a build-in rule. |
Please put together a proposal for the rule per the link. |
I must start by saying that I have very strict guidelines on what can be thrown, so this might be far to restrictive for others. I would want this rule to basically enforce only throwing errors in the form The rule should be disabled by default, could be part of the
|
👍 |
There's a lot more details to hash out here. We can easily flag patterns like: throw "foo"; We can also notice throw new Error(); What we can't easily do is notice var err = new Error();
throw err; I don't know that it's worth trying to limit the constructor being used given that. |
As I mentioned, I would be fine to only allow |
The larger question around this rule is this: should it disallow specific patterns or allow specific patterns? |
I would only allow one pattern |
If it will allow only one pattern (throw new Error), you will have issues with code like this: try {
....
} catch (e) {
....
throw e;
} This feels specific enough to your coding style to be a custom rule, instead of going into main repo. |
Only allowing one pattern is far too limiting. I'm leaning more towards looking for specific bad examples and warning them. |
I agree with @ilyavolodin that the pattern try {
....
} catch (e) {
....
throw e;
} is so common that it should also be allowed, but I'm not so sure if there are other reasonably common code pattern for throwing exceptions. Am I guessing right, that supporting the above pattern would be be possible in eslint by matching the argument |
I'm not in favor of ESLint detecting such a pattern. We know from experience that there are frequently an unknown number of exception cases when we try to detect things, which is why we opt for detecting small, known cases rather than trying to account for everything. I'm getting closer to being convinced that we should simply detect |
Oh ha I already built this a few weeks ago. I should have mentioned it :) Here's how mine would ideally work:
(may revisit this with more thoughts, but +1000 I love this rule) |
New: rule no-throw-literal added (fixes #1791)
is there a rule to make sure people don't throw strings?
The text was updated successfully, but these errors were encountered: