Skip to content

use the more specific TypeError objects for function parameters errors. #243

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

Merged
merged 1 commit into from
Apr 16, 2018
Merged

use the more specific TypeError objects for function parameters errors. #243

merged 1 commit into from
Apr 16, 2018

Conversation

hhromic
Copy link
Contributor

@hhromic hhromic commented Apr 12, 2018

[this PR is a rebase (and update) of PR #157]

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, it is suggested that:

  1. 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 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.

@mbroadst
Copy link
Member

@hhromic hi, sorry one last thing, please don't commit the update to the browser build. That will be updated automatically as part of the build process. Thanks!

@hhromic
Copy link
Contributor Author

hhromic commented Apr 16, 2018

Hi, thanks for the clarification! Indeed I was confused if I should have updated the browser build or not.
I reverted that in the PR now. Cheers!

…ors.

[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]>
@mbroadst
Copy link
Member

@hhromic thanks for the contribution!

@mbroadst mbroadst merged commit e068b5c into mongodb:2.0.0 Apr 16, 2018
@hhromic
Copy link
Contributor Author

hhromic commented Apr 16, 2018

happy to help!

@hhromic hhromic deleted the specific-type-errors branch April 16, 2018 14:20
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