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

throwing non-errors (specifically strings) #1791

Closed
jonathanong opened this issue Feb 8, 2015 · 16 comments
Closed

throwing non-errors (specifically strings) #1791

jonathanong opened this issue Feb 8, 2015 · 16 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion triage An ESLint team member will look at this issue soon

Comments

@jonathanong
Copy link

is there a rule to make sure people don't throw strings?

@nzakas
Copy link
Member

nzakas commented Feb 8, 2015

We do not. For future reference, our rules are all documented: http://eslint.org/docs/rules/

@nzakas nzakas added the triage An ESLint team member will look at this issue soon label Feb 8, 2015
@jonathanong
Copy link
Author

Well this is a feature request then :)

@nzakas
Copy link
Member

nzakas commented Feb 8, 2015

Please take a moment to read over what new rule proposals should include: http://eslint.org/docs/developer-guide/contributing.html#new-rules

@doberkofler
Copy link
Contributor

I would very much like to see this as a build-in rule.

@nzakas
Copy link
Member

nzakas commented Feb 8, 2015

Please put together a proposal for the rule per the link.

@doberkofler
Copy link
Contributor

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 throw new Error() where Error should be one of an arbitrary list of constructors that can be specified in the rule definition.

The rule should be disabled by default, could be part of the Best Practices and could be specified with something like this:

"throw-style": [1, { "force-new": true, "constructors": ["Error", "TypeError", "CustomError"]}]

@gcochard
Copy link
Contributor

👍

@nzakas
Copy link
Member

nzakas commented Feb 10, 2015

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.

@doberkofler
Copy link
Contributor

As I mentioned, I would be fine to only allow throw new Error() as I have very strict rules as I almost never use any other form of exception. Clearly for others this might be to restrictive.
Looking at the code I see around throwing a variable does not seem to very common.

@nzakas
Copy link
Member

nzakas commented Feb 10, 2015

The larger question around this rule is this: should it disallow specific patterns or allow specific patterns?

@doberkofler
Copy link
Contributor

I would only allow one pattern

@ilyavolodin
Copy link
Member

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.

@nzakas
Copy link
Member

nzakas commented Feb 10, 2015

Only allowing one pattern is far too limiting. I'm leaning more towards looking for specific bad examples and warning them.

@doberkofler
Copy link
Contributor

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 e with the object e that will be thrown ?

@nzakas
Copy link
Member

nzakas commented Feb 10, 2015

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 throw followed by a literal and then warn that an error should be used.

@xjamundx
Copy link
Contributor

Oh ha I already built this a few weeks ago. I should have mentioned it :) Here's how mine would ideally work:

  • you can always re-throw params
  • basic variable watching (within the file) to ensure that the variable you're throwing was declared as an error or a param
  • support for new CustomError or whatever as well (we extend errors all the time here), either through a whitelist or just basically assume new Thing is an error
  • Would also strongly consider warning against cb("ljkasjklds") or done("alskjjlkds").

(may revisit this with more thoughts, but +1000 I love this rule)

@nzakas nzakas closed this as completed in 5f7d82d Feb 18, 2015
nzakas added a commit that referenced this issue Feb 18, 2015
New: rule no-throw-literal added (fixes #1791)
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion triage An ESLint team member will look at this issue soon
Projects
None yet
Development

No branches or pull requests

6 participants