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: refactor invalidArgType() #15544

Closed
wants to merge 1 commit into from

Conversation

Trott
Copy link
Member

@Trott Trott commented Sep 21, 2017

invalidArgType() uses includes() in two places where startsWith()
and endsWith() are more appropriate (at least in my opinion). Switch
to those more specific functions.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

errors

`invalidArgType()` uses `includes()` in two places where `startsWith()`
and `endsWith()` are more appropriate (at least in my opinion). Switch
to those more specific functions.
@Trott Trott added the errors Issues and PRs related to JavaScript errors originated in Node.js core. label Sep 21, 2017
@@ -320,7 +320,7 @@ function invalidArgType(name, expected, actual) {
if (Array.isArray(name)) {
var names = name.map((val) => `"${val}"`).join(', ');
msg = `The ${names} arguments ${determiner} ${oneOf(expected, 'type')}`;
} else if (name.includes(' argument')) {
} else if (name.endsWith(' argument')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason not to check the type here too?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's checked in the if part. I mean, I guess it could be something other than a string or an array, but it's an internal function, so I'm not worried about end users sending an object or something.

My preference here is slight, though, and I can see the point of programming extra defensively here and adding the type check. If I do that, I'll want to add a test too to cover it.

Copy link
Contributor

@refack refack Sep 23, 2017

Choose a reason for hiding this comment

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

I'm also -0 on over guarding an internal function.
IMHO if at all it should be at asserted around L309:

node/lib/internal/errors.js

Lines 307 to 309 in adbed02

function invalidArgType(name, expected, actual) {
internalAssert(name, 'name is required');

@@ -320,7 +320,7 @@ function invalidArgType(name, expected, actual) {
if (Array.isArray(name)) {
var names = name.map((val) => `"${val}"`).join(', ');
msg = `The ${names} arguments ${determiner} ${oneOf(expected, 'type')}`;
} else if (name.includes(' argument')) {
} else if (name.endsWith(' argument')) {
Copy link
Contributor

@refack refack Sep 23, 2017

Choose a reason for hiding this comment

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

I'm also -0 on over guarding an internal function.
IMHO if at all it should be at asserted around L309:

node/lib/internal/errors.js

Lines 307 to 309 in adbed02

function invalidArgType(name, expected, actual) {
internalAssert(name, 'name is required');

@@ -309,7 +309,7 @@ function invalidArgType(name, expected, actual) {

// determiner: 'must be' or 'must not be'
let determiner;
if (expected.includes('not ')) {
if (typeof expected === 'string' && expected.startsWith('not ')) {
Copy link
Member

Choose a reason for hiding this comment

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

This should always be a string. A failure would be totally fine out of my perspective as we should have tests for this anyway.
But I think it is not really important for now.

@BridgeAR
Copy link
Member

@BridgeAR
Copy link
Member

Landed in cef6e1c

@BridgeAR BridgeAR closed this Sep 24, 2017
BridgeAR pushed a commit that referenced this pull request Sep 24, 2017
`invalidArgType()` uses `includes()` in two places where `startsWith()`
and `endsWith()` are more appropriate (at least in my opinion). Switch
to those more specific functions.

PR-URL: #15544
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@jasnell
Copy link
Member

jasnell commented Sep 25, 2017

This does not land cleanly in v8.x. I believe it depends on a previously landed semver-major but not entirely sure. Would need a backport if it's relevant.

addaleax pushed a commit to addaleax/ayo that referenced this pull request Sep 30, 2017
`invalidArgType()` uses `includes()` in two places where `startsWith()`
and `endsWith()` are more appropriate (at least in my opinion). Switch
to those more specific functions.

PR-URL: nodejs/node#15544
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@Trott Trott deleted the refactor-internal-errors branch January 13, 2022 22:46
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants