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

Error.name vs Error.code #789

Closed
stouf opened this issue Aug 24, 2017 · 5 comments
Closed

Error.name vs Error.code #789

stouf opened this issue Aug 24, 2017 · 5 comments
Labels

Comments

@stouf
Copy link

stouf commented Aug 24, 2017

  • Node.js Version: >=8.3.0
  • OS: Any
  • Scope (install, code, runtime, meta, other?): Best practice
  • Module (and version) (if relevant): Errors

There seem to be an ongoing effort in node.js for getting any error thrown by the code functions to have a code property (see this pull request or this issue for example).

As a node.js user, I'm used to use the name property to programmatically identify errors in the code I write. For example,

// user.js
async function getByEmail(email) {
  const resultSet = await db.getBy('user', { email });
  if (resultSet.length === 0) {
    const error = new Error(`No user with the email ${email} could be found`);
    error.name = 'EMAIL_NOT_FOUND';
    throw error;
  }
  return resultSet;
}


// In some business logic code
user.getByEmail(inputEmail)
  .then((resultSet) => {
    return `Are you one of those? ${resultSet.map(x => x.name).join(',')}`;
  })
  .catch((error) => {
    if (error.name === 'EMAIL_NOT_FOUND') {
      return 'No user could be found for the input email';
    }
    throw error;
  });

Although the name property is not mentioned in Node.js documentation about Errors, it is a standard property and Node.js documentation about Errors tells us the following:

All JavaScript and System errors raised by Node.js inherit from, or are instances of, the standard JavaScript Error class and are guaranteed to provide at least the properties available on that class.

Now that the code is property is mentioned in Node.js documentation, I am wondering which of code and name I should use as a user for programmatically identifying errors.

Is there anything one should be aware of? Does the node.js team encourage for the use of one over the other?

@jasnell
Copy link
Member

jasnell commented Aug 24, 2017

For core, especially given that we are still in the process of migrating errors, use code first, but if code is not present, use name

@jasnell
Copy link
Member

jasnell commented Aug 24, 2017

Answer part 2 .... unfortunately many errors cannot be determined just by their name and require looking into the error message to understand what happened... which is why we're introducing code

@stouf
Copy link
Author

stouf commented Aug 24, 2017

Thanks for responding :)

I see. What about it for people building their own applications in Node.js and aiming at programmatically identifying the errors their own code creates? Would the node.js team encourage using code or name for that? Or maybe it's just up to the developers and there is no such thing as good practice for that?

@tniessen
Copy link
Member

tniessen commented Sep 5, 2017

Our goal is to make the code property the optimal way to distinguish and identify errors.

aiming at programmatically identifying the errors their own code creates?

We are working on a good error handling strategy. There has already been a similar question at nodejs/node#15057 and the discussion is being continued in nodejs/CTC#173.

@tniessen tniessen closed this as completed Sep 5, 2017
@stouf
Copy link
Author

stouf commented Sep 6, 2017

@tniessen Awesome, thanks for the reference 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants