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

url: port WHATWG URL API to internal/errors #12574

Closed
wants to merge 3 commits into from

Conversation

TimothyGu
Copy link
Member

Refs: #11273
Refs: #11299

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

url, errors

@nodejs-github-bot nodejs-github-bot added dont-land-on-v4.x errors Issues and PRs related to JavaScript errors originated in Node.js core. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Apr 21, 2017
@@ -561,6 +561,13 @@ found [here][online].
<a id="nodejs-error-codes"></a>
## Node.js Error Codes

<a id="ERR_ARG_NOT_ITERABLE"></a>
Copy link
Member

Choose a reason for hiding this comment

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

This can also be just ERR_INVALID_ARG_TYPE with Iterable as the expected type..no strong feelings about it though.

Copy link
Member

Choose a reason for hiding this comment

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

For this I think the more specific error is warranted.

@@ -840,15 +841,17 @@ class URLSearchParams {
for (const pair of init) {
if (typeof pair !== 'object' ||
typeof pair[Symbol.iterator] !== 'function') {
throw new TypeError('Each query pair must be iterable');
throw new errors.TypeError('ERR_INVALID_TUPLE', 'Each query pair',
Copy link
Member

Choose a reason for hiding this comment

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

I know the name/value thing is already in error messages...but since we are changing error messages anyway, I think [name, value] would be easier to understand at the first glance.

### ERR_INVALID_THIS

The `'ERR_INVALID_THIS'` error code is used generically to identify that a
Node.js API function is called with an incompatible `this` value. Its use is
Copy link
Member

Choose a reason for hiding this comment

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

The "limited to WHATWG URL" part can be left out IMO, the source of errors would be visible in the stack trace anyway... This way people don't need to change this section when they add this type of error to other APIs


An error with code `'ERR_INVALID_TUPLE'` is thrown when an element in the
`iterable` provided to the [WHATWG][WHATWG URL API] [`URLSearchParams`
constructor][`new URLSearchParams(iterable)`] does not represent a name/value
Copy link
Member

Choose a reason for hiding this comment

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

[name, value] tuple would be easier to grasp..

Copy link
Member Author

@TimothyGu TimothyGu Apr 24, 2017

Choose a reason for hiding this comment

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

"name/value tuple" is the WHATWG nomenclature, but I'm perfectly fine with "[name, value] tuple".

An error using the `'ERR_INVALID_URL'` code is thrown when an invalid URL is
passed to the [WHATWG][WHATWG URL API] [`URL` constructor][`new URL(input)`] to
be parsed. The thrown error object typically has an additional property
`'input'` that contains the URL that failed to parse.
Copy link
Member

Choose a reason for hiding this comment

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

Note that at the moment input might not be exactly the same as the original input since it needs to go through trimming and String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8 first...we could've stored the original input, but that's for another PR to consider

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, the REPLACE_INVALID_UTF8 is pretty edge-case, but trimming can indeed be something the user does not expect. Agreed on addressing this in a separate PR though.

in the [WHATWG URL API][] for strict compliance with the specification (which
in some cases may accept `func(undefined)` but not `func()`). In most native
Node.js APIs, `func(undefined)` and `func()` are treated identically, and the
[`ERR_INVALID_ARG_TYPE`][] error code may be used instead.
Copy link
Member

Choose a reason for hiding this comment

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

In that case, why not just use 'ERR_INVALID_ARG_TYPE'?

Copy link
Member Author

Choose a reason for hiding this comment

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

'ERR_INVALID_ARG_TYPE' has the following templated message:

The "FOO" argument must be of type BAR.

In this case, it is not the question of what type the argument is, but whether it is defined at all.

E('ERR_ASSERTION', (msg) => msg);
E('ERR_INVALID_ARG_TYPE', invalidArgType);
E('ERR_INVALID_CALLBACK', 'callback must be a function');
E('ERR_INVALID_FILE_URL_HOST', 'File URL host must %s');
E('ERR_INVALID_FILE_URL_PATH', 'File URL path must %s');
E('ERR_INVALID_THIS', 'Value of this must be of type %s');
Copy link
Member

Choose a reason for hiding this comment

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

Maybe surround this with quotes or backticks? The original error message uses backticks.

Copy link
Member Author

@TimothyGu TimothyGu Apr 25, 2017

Choose a reason for hiding this comment

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

It's quite rare to have an error message with backticks in it (most error messages use double quotes). Here are a few alternatives:

  1. Value of "this" must be of type %s
  2. Method called on incompatible receiver of type %s (V8-inspired)
  3. Method called on incompatible %s (SpiderMonkey-inspired)

Choice 1 SGTM.

E('ERR_INVALID_THIS', 'Value of this must be of type %s');
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple');
E('ERR_INVALID_URL',
(input) => `Invalid URL${input !== undefined ? `: ${input}` : ''}`);
Copy link
Member

Choose a reason for hiding this comment

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

I think undefined should be in the message here..otherwise if the user makes a mistake and passes an undefined, there is no way to know if it is an undefined or just an empty string.

return new TypeError(
'Path must not include encoded \\ or / characters');
return new errors.TypeError('ERR_INVALID_FILE_URL_PATH',
'not include encoded \\ or / characters');
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit weird to put the verbs in separate places...maybe the must part can be left out in the error message format and written here?

return typeof err === 'object' && 'name' in err && err.name === code.name;
return typeof err === 'object' &&
'name' in err &&
err.name.startsWith(code.name);
Copy link
Member

Choose a reason for hiding this comment

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

Or maybe err instanceof code.constructor ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know about this one, I'm generally not a huge fan of instanceof checks due to unreliability across different V8 contexts, and it is quite possible that WPT's testharness.js uses the error name string check instead of instanceof.

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

I'd like to see @joyeecheung's comments addressed.

@TimothyGu
Copy link
Member Author

Addressed most of @joyeecheung's comments, except those that are still visible above.

@joyeecheung
Copy link
Member

@TimothyGu
Copy link
Member Author

Landed in d457a98.

@TimothyGu TimothyGu closed this Apr 25, 2017
@TimothyGu TimothyGu deleted the url-errors branch April 25, 2017 23:34
TimothyGu added a commit that referenced this pull request Apr 25, 2017
Also slightly revises grammar.

PR-URL: #12574
Refs: #11273
Refs: #11299
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
@jasnell jasnell mentioned this pull request May 11, 2017
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. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants