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

commented Apr 21, 2017

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

@@ -561,6 +561,13 @@ found [here][online].
<a id="nodejs-error-codes"></a>
## Node.js Error Codes

<a id="ERR_ARG_NOT_ITERABLE"></a>

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

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

This comment has been minimized.

Copy link
@jasnell

jasnell Apr 24, 2017

Member

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',

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

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.

doc/api/errors.md Outdated
### 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

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

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

doc/api/errors.md Outdated

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

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

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

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Apr 24, 2017

Author Member

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

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

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

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Apr 24, 2017

Author Member

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.

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

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

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Apr 24, 2017

Author Member

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

lib/internal/errors.js Outdated
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');

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

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

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Apr 25, 2017

Author Member

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.

lib/internal/errors.js Outdated
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}` : ''}`);

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

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.

lib/internal/url.js Outdated
return new TypeError(
'Path must not include encoded \\ or / characters');
return new errors.TypeError('ERR_INVALID_FILE_URL_PATH',
'not include encoded \\ or / characters');

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

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);

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Apr 24, 2017

Member

Or maybe err instanceof code.constructor ?

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Apr 25, 2017

Author Member

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.

@jasnell
Copy link
Member

left a comment

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

TimothyGu added 3 commits Apr 21, 2017
errors: factor out "of type" text generation
Also slightly revise the grammar of two-expected value scenario.

@TimothyGu TimothyGu force-pushed the TimothyGu:url-errors branch to b43cdee Apr 25, 2017

@TimothyGu

This comment has been minimized.

Copy link
Member Author

commented Apr 25, 2017

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

@joyeecheung

This comment has been minimized.

@TimothyGu

This comment has been minimized.

Copy link
Member Author

commented Apr 25, 2017

Landed in d457a98.

@TimothyGu TimothyGu closed this Apr 25, 2017

@TimothyGu TimothyGu deleted the TimothyGu:url-errors branch Apr 25, 2017

TimothyGu added a commit that referenced this pull request Apr 25, 2017
url: port WHATWG URL API to internal/errors
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 referenced this pull request May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
5 participants
You can’t perform that action at this time.