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

errors: add ERR_INVALID_OPT_TYPE and support options in validators.js #31251

Closed

Conversation

lundibundi
Copy link
Member

Description in the commit messages.

Also, I considered adding helper functions like validateBufferOpt to avoid having to write an object with type: 'option' every time, WDYT?

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added (do we have a doc for validators.js?)
  • commit message follows commit guidelines

/cc @BridgeAR

@nodejs-github-bot nodejs-github-bot added the errors Issues and PRs related to JavaScript errors originated in Node.js core. label Jan 7, 2020
Comment on lines +696 to +697
if (valueType !== undefined) {
msg += `"${name}" ${valueType} `;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing added to this function (along with the fourth argument), otherwise a copy of the ERR_INVALID_ARG_TYPE function.

@nodejs-github-bot
Copy link
Collaborator

@BridgeAR
Copy link
Member

BridgeAR commented Jan 7, 2020

I actually forgot that ERR_INVALID_ARG_TYPE already has some kind of support for properties: By passing through options.foobar the error message prints property instead of argument.

@BridgeAR
Copy link
Member

BridgeAR commented Jan 7, 2020

It does of course not change the error code, so it's still a bit different. It would have probably been ideal to name the error ERR_INVALID_TYPE and then just add the information about being an argument or property in the message. But that's quite a breaking change...

I have to think about adding a completely new error code as implemented here. So far I have not made up my mind if I think it's good or bad.

@lundibundi
Copy link
Member Author

It does of course not change the error code, so it's still a bit different. It would have probably been ideal to name the error ERR_INVALID_TYPE and then just add the information about being an argument or property in the message. But that's quite a breaking change...

That's what I was thinking initially but considering the amount of ERR_INVALID_ARG_TYPE errors we already have decided that it was not worth it, the breakage is quite large for a 'generalization' change. Though I'd actually prefer this approach if we can make it.

@tniessen
Copy link
Member

Context: #31178 (comment)

This adds `ERR_INVALID_OPT_TYPE` error that is the equivalent of
`ERR_INVALID_ARG_TYPE` for options.
`name` argument is now able to be and object with a `name` and `type`
keys. If `type` is `'option'` then `ERR_INVALID_OPT_TYPE` and
`ERR_INVALID_OPT_VALUE` errors will be used.
@lundibundi
Copy link
Member Author

Rebased and fixed conflicts.

ping @BridgeAR @Trott @jasnell (is there a nodejs/ group for this kind of change?).

@jasnell
Copy link
Member

jasnell commented Jan 21, 2020

(is there a nodejs/ group for this kind of change?

For error related PRs? Not really

@jasnell
Copy link
Member

jasnell commented Jan 21, 2020

Overall I am fine with having a separate error code for options types but changing the code for existing errors makes this a semver-major.

@jasnell jasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Jan 21, 2020
@lundibundi
Copy link
Member Author

lundibundi commented Jan 21, 2020

@jasnell this doesn't change the existing error, the behavior should be exactly the same for ERR_INVALID_ARG_TYPE. I want this to be completely compatible so if you spotted a breaking change please tell me.

@lundibundi lundibundi removed the semver-major PRs that contain breaking changes and should be released in the next major version. label Feb 9, 2020
@lundibundi
Copy link
Member Author

ping @BridgeAR @Trott @jasnell.

@lundibundi lundibundi added the review wanted PRs that need reviews. label Feb 9, 2020
@nodejs-github-bot
Copy link
Collaborator

@tniessen
Copy link
Member

So now we have ERR_INVALID_OPT_TYPE and ERR_INVALID_OPT_VALUE, but some values will throw other errors, e.g., ERR_OUT_OF_RANGE? This seems more and more confusing to me.

@lundibundi
Copy link
Member Author

@tniessen I agree that it's not the best solution, but that's what we already have (with ERR_INVALID_ARG_TYPE etc), this PR just expands that to allow handling of option errors in the same way.

The current idea is that if we know a more specific error (e.g. out of range) then we use it, otherwise we just throw a generic invalid value or invalid type.
TBH I don't really see a point in specific out of range error in our current approach and IMO we could just always use invalid value and invalid type with specific error messages.

@Trott
Copy link
Member

Trott commented Feb 18, 2020

Pinging a few people based on git shortlog -n -s lib/internal/errors.js. @joyeecheung @addaleax @targos

@BridgeAR BridgeAR force-pushed the master branch 2 times, most recently from 8ae28ff to 2935f72 Compare May 31, 2020 12:19
@lundibundi
Copy link
Member Author

ping @Trott @BridgeAR @jasnell @addaleax @targos @joyeecheung @tniessen
Should I rebase this to continue the discussion or did we hit a dead end as there doesn't seem to be enough attention to this one?

@BridgeAR
Copy link
Member

I personally would keep it as is and I'm +-0 to this. What we do need is something to differentiate different error groups e.g., input validation errors, network errors, file system error, not supported error and similar.

@jasnell
Copy link
Member

jasnell commented Jun 19, 2020

Not sure what the status is on this one but it needs a rebase

@lundibundi
Copy link
Member Author

I can rebase this if we decide to move on with this, unfortunately, there is not enough feedback.
I'd prefer to have it to be able to streamline args\options handling and avoid discrepancies in errors.

@lundibundi
Copy link
Member Author

Will try moving in the other direction with this.
#34070 (comment)

@lundibundi lundibundi closed this Aug 7, 2020
lundibundi added a commit to lundibundi/node that referenced this pull request Aug 18, 2020
This will be a start to generalize all argument validation
errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more
specific errors.
The OPT errors didn't bring much to the errors as it's just another
variant of ARG error which is sometimes more confusing (some of our code
used OPT errors to denote just argument validation errors presumably
because of similarity of OPT to 'option' and not 'options-object')
and they don't specify the name of the options object where the invalid
value is located. Much better approach would be to just specify path
to the invalid value in the name of the value as it is done in this PR
(i.e. 'options.format', 'options.publicKey.type' etc)

Also since this decreases a variety of errors we have it'd be easier to
reuse validation code across the codebase.

Refs: nodejs#31251
Refs: nodejs#34070 (comment)
Signed-off-by: Denys Otrishko <shishugi@gmail.com>
lundibundi added a commit that referenced this pull request Sep 11, 2020
This will be a start to generalize all argument validation
errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more
specific errors.
The OPT errors didn't bring much to the errors as it's just another
variant of ARG error which is sometimes more confusing (some of our code
used OPT errors to denote just argument validation errors presumably
because of similarity of OPT to 'option' and not 'options-object')
and they don't specify the name of the options object where the invalid
value is located. Much better approach would be to just specify path
to the invalid value in the name of the value as it is done in this PR
(i.e. 'options.format', 'options.publicKey.type' etc)

Also since this decreases a variety of errors we have it'd be easier to
reuse validation code across the codebase.

Refs: #31251
Refs: #34070 (comment)
Signed-off-by: Denys Otrishko <shishugi@gmail.com>

PR-URL: #34682
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
joesepi pushed a commit to joesepi/node that referenced this pull request Jan 8, 2021
This will be a start to generalize all argument validation
errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more
specific errors.
The OPT errors didn't bring much to the errors as it's just another
variant of ARG error which is sometimes more confusing (some of our code
used OPT errors to denote just argument validation errors presumably
because of similarity of OPT to 'option' and not 'options-object')
and they don't specify the name of the options object where the invalid
value is located. Much better approach would be to just specify path
to the invalid value in the name of the value as it is done in this PR
(i.e. 'options.format', 'options.publicKey.type' etc)

Also since this decreases a variety of errors we have it'd be easier to
reuse validation code across the codebase.

Refs: nodejs#31251
Refs: nodejs#34070 (comment)
Signed-off-by: Denys Otrishko <shishugi@gmail.com>

PR-URL: nodejs#34682
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
errors Issues and PRs related to JavaScript errors originated in Node.js core. review wanted PRs that need reviews.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants