Skip to content

use more specific TypeError objects for function parameters errors #157

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

Closed
wants to merge 1 commit into from
Closed

Conversation

hhromic
Copy link
Contributor

@hhromic hhromic commented Dec 10, 2015

Hi,
here is the PR for the proposed changes commented in here. I went further and modified all other functions in the codebase where this specific error object makes sense (function parameters checking). So the error handling in the lib is consistent.

Cheers and thanks!
Hugo.

@mbroadst
Copy link
Member

mbroadst commented Apr 6, 2018

@hhromic can you please fix the conflicts in this PR so we can merge this? Apologies for taking so longer to get around to this.

@hhromic
Copy link
Contributor Author

hhromic commented Apr 6, 2018

no problem @mbroadst !
However, I'm confused on which branch I should send the PR to. In the original discussion we had (#156), it was mentioned that no changes will be accepted until at least version 5.x. However the current default branch is 1.0-branch.
Can you confirm me to which brand I should rebase the patch for?

@mbroadst
Copy link
Member

mbroadst commented Apr 6, 2018

@hhromic yeah I saw that issue as well and was very confused by it. We are currently in the 2.x series, and thats what you should rebase to, the 2.0.0 branch. Thanks!

@hhromic
Copy link
Contributor Author

hhromic commented Apr 12, 2018

(closing as new PR is in place now)

@hhromic hhromic closed this Apr 12, 2018
mbroadst pushed a commit that referenced this pull request Apr 16, 2018
[this PR is a rebase (and update) of PR #157]

For example, when constructing a new `ObjectId` object, the passed id value is validated for conversion to an hex string.
This is just fine, however a generic Error object is thrown when this validation fails.
Tracking the error then becomes unconvenient.
From official [Joyent's design patterns for errors](https://www.joyent.com/developers/node/design/errors), it is suggested that:

> 3. Use the Error's name property to distinguish errors programmatically.
>    When you need to figure out what kind of error this is, use the name property.
>    Built-in JavaScript names you may want to reuse include "RangeError"
>    (an argument is outside of its valid range) and "TypeError" (an argument has the wrong type).
>    For HTTP errors, it's common to use the RFC-given status text to name the error, like
>    "BadRequestError" or "ServiceUnavailableError".

This PR uses the [`TypeError` class](https://nodejs.org/api/errors.html#errors_class_typeerror) instead of the generic `Error` object.
Because `TypeError` is a subclass of `Error`, all existing code should continue to work flawlessly and new code can more easily track an invalid argument error.

Signed-off-by: Hugo Hromic <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants