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

http_client, errors: migrate to use internal/errors #14423

Closed

Conversation

starkwang
Copy link
Contributor

@starkwang starkwang commented Jul 22, 2017

Ref: #11273

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)

errors, http_client

@nodejs-github-bot nodejs-github-bot added errors Issues and PRs related to JavaScript errors originated in Node.js core. http Issues or PRs related to the http subsystem. labels Jul 22, 2017
@refack
Copy link
Contributor

refack commented Jul 22, 2017

@starkwang thank you so much for helping with this!

We recently updated the recommended way to test internal Errors — https://github.com/nodejs/node/pull/14207/files
tl;dr need to test the message only if it's variable, and once is enough (in test/parallel/test-internal-errors.js).
IMHO the PR is good as is, but it would be great if you followed up and made it even better.

/cc @jasnell @fhinkel @Trott @mhdawson

@refack
Copy link
Contributor

refack commented Jul 22, 2017

E('ERR_INVALID_FD', '"fd" must be a positive integer: %s');
E('ERR_INVALID_FILE_URL_HOST',
'File URL host must be "localhost" or empty on %s');
E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s');
E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent');
E('ERR_INVALID_HTTP_TOKEN', (name) => `${name} must be a valid HTTP token`);
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI you could just use %s place holders, but IMHO this way is more readable.

Copy link
Member

Choose a reason for hiding this comment

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

But: consistency 😉

Copy link
Contributor

Choose a reason for hiding this comment

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

There is none anyway...
I'll open a PR to change them all.

@tniessen
Copy link
Member

Whoever lands this: I think the subsystem should be http_client with an underscore.

@starkwang starkwang force-pushed the http-client-internal-errors branch from 6031cca to 4ad89b7 Compare July 22, 2017 16:29
@starkwang
Copy link
Contributor Author

@refack I've added some tests for the new errors in test/parallel/test-internal-errors.js😀

@Trott Trott added the semver-major PRs that contain breaking changes and should be released in the next major version. label Jul 22, 2017
@Trott
Copy link
Member

Trott commented Jul 22, 2017

semver-major so pinging @nodejs/ctc for some reviews

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

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

💯

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

@addaleax
Copy link
Member

@starkwang Sorry to be a bummer, but can you rebase this against master? There are some merge conflicts here…

@starkwang starkwang force-pushed the http-client-internal-errors branch 2 times, most recently from c54858b to 4c62748 Compare July 27, 2017 02:20
@@ -597,6 +597,7 @@ Used when `Console` is instantiated without `stdout` stream or when `stdout` or
Used when the native call from `process.cpuUsage` cannot be processed properly.

<a id="ERR_DNS_SET_SERVERS_FAILED"></a>
### ERR_DNS_SET_SERVERS_FAILED
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that I missing some titles for errors.md in #14212 . So I added the missing titles in this PR

<a id="ERR_INVALID_IP_ADDRESS"></a>
### ERR_INVALID_IP_ADDRESS
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that I missing some titles for errors.md in #14212 . So I added the missing titles in this PR

@starkwang
Copy link
Contributor Author

@addaleax I've just rebased the branch : )

@addaleax
Copy link
Member

Copy link
Member

@tniessen tniessen left a comment

Choose a reason for hiding this comment

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

From using-internal-errors.md:

If the error message is not a constant string then tests to validate the formatting of the message based on the parameters used when creating the error should be added to test/parallel/test-internal-errors.js. These tests should validate all of the different ways parameters can be used to generate the final message string.

And this section:

In addition, there should also be tests which validate the use of the error based on where it is used in the codebase. For these tests, except in special cases, they should only validate that the expected code is received and NOT validate the message.

I don't exactly care about this, just remember that this PR does not strictly comply with these guidelines.

Thanks for your efforts @starkwang! 😃

}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /The "method" argument must be of type string/
Copy link
Member

Choose a reason for hiding this comment

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

Unless I am missing something, it does not appear to be necessary to use a regular expression here, a string literal should work just fine. By the way, if you want to match an exact string using regular expressions, use start (^) and end markers ($). Otherwise, the match will succeed even if there is content before or after the expression:

const expr = /The "method" argument must be of type string/;
const str = 'The "method" argument must be of type string. Also, this sentence should not be here';
expr.test(str) === true

code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: new RegExp('The "options\\.hostname" property must be one of ' +
'type string, undefined, or null')
Copy link
Member

Choose a reason for hiding this comment

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

Same here, why is this a regular expression?

code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: new RegExp('The "options\\.host" property must be one of ' +
'type string, undefined, or null')
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

@starkwang starkwang force-pushed the http-client-internal-errors branch 3 times, most recently from 58fd145 to d4c91a7 Compare July 28, 2017 02:42
@starkwang starkwang changed the title http client, errors: migrate to use internal/errors http_client, errors: migrate to use internal/errors Jul 28, 2017
<a id="ERR_RENDER_HEADERS_FAILED"></a>
### ERR_RENDER_HEADERS_FAILED

Used when http headers has been sent before rendering the headers.
Copy link
Member

Choose a reason for hiding this comment

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

Grammar: have been sent. Also, http probably refers to the protocol instead of the module name, so I would prefer HTTP over http.

Copy link
Contributor

Choose a reason for hiding this comment

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

Used when new HTTP headers are added to a response, after the headers part has already been sent.

<a id="ERR_INVALID_HTTP_TOKEN"></a>
### ERR_INVALID_HTTP_TOKEN

Used when `options.method` received an invalid http token.
Copy link
Member

Choose a reason for hiding this comment

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

http probably refers to the protocol instead of the module name, so I would prefer HTTP over http.

<a id="ERR_INVALID_DOMAIN_NAME"></a>
### ERR_INVALID_DOMAIN_NAME

Used when `url.parse()` failed to parse the `host` or `hostname` that be passed to
Copy link
Member

Choose a reason for hiding this comment

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

Grammar: that be passed. I would just say "... failed to parse the host or hostname option".

Copy link
Contributor

Choose a reason for hiding this comment

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

Used when `hostname` can not be parsed from a provided URL.

<a id="ERR_UNESCAPED_CHARACTERS"></a>
### ERR_UNESCAPED_CHARACTERS

Used when receive a string that contains unescaped characters.
Copy link
Member

Choose a reason for hiding this comment

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

Grammar: when receive. Should probably be when receiving.

Copy link
Contributor

@refack refack Jul 29, 2017

Choose a reason for hiding this comment

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

Used when a string that contains unescaped characters was received.

@@ -303,7 +302,7 @@ ClientRequest.prototype._finish = function _finish() {

ClientRequest.prototype._implicitHeader = function _implicitHeader() {
if (this._header) {
throw new Error('Can\'t render headers after they are sent to the client');
throw new errors.Error('ERR_RENDER_HEADERS_FAILED');
Copy link
Member

Choose a reason for hiding this comment

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

I don't have a strong opinion on this, I would prefer a more meaningful name for this error code. From the error code, the actual reason of the error is unclear, and the message implies that having the headers sent already is the only reason sending headers can fail, ever, and I am not too sure about that. Maybe something like ERR_HEADERS_SENT? Doesn't sound perfect either... cc @refack

Copy link
Contributor

Choose a reason for hiding this comment

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

ERR_HTTP_HEADERS_SENT

@refack
Copy link
Contributor

refack commented Jul 29, 2017

IMHO the documentation improvements should be non blockers, since simply migrating the Errors is of higher priority. But it's definatly nice to have while we wait for another @nodejs/ctc approval.

@Trott
Copy link
Member

Trott commented Jul 29, 2017

This PR has http, internal errors, and is semver major. Seems like @jasnell should be all over it. Ping! :-D

@starkwang starkwang force-pushed the http-client-internal-errors branch from d4c91a7 to db8fb2a Compare July 30, 2017 13:41
@starkwang
Copy link
Contributor Author

Pushed commit to address comments

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

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

💯

@refack
Copy link
Contributor

refack commented Aug 1, 2017

@tniessen
Copy link
Member

tniessen commented Aug 1, 2017

Landed in bdfbce9.

@tniessen tniessen closed this Aug 1, 2017
tniessen pushed a commit that referenced this pull request Aug 1, 2017
PR-URL: #14423
Refs: #11273
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
MylesBorins added a commit to MylesBorins/node that referenced this pull request Sep 13, 2017
This error code originally landed in a semver-major commit and is used
by the ESM implementation. This backport includes the error message
and the documentation for the error.

I did attempt to write a test for this, but it did not seem possible
to catch an exception during import, I was also unable to execute
`node --experimental-modules` properly inside of a child_process.

I'll dig more into getting a test together, but we should backport
this fix in the mean time.

Refs: nodejs#14423
Fixes: nodejs#15374
MylesBorins added a commit that referenced this pull request Sep 14, 2017
This error code originally landed in a semver-major commit and is used
by the ESM implementation. This backport includes the error message
and the documentation for the error.

I did attempt to write a test for this, but it did not seem possible
to catch an exception during import, I was also unable to execute
`node --experimental-modules` properly inside of a child_process.

I'll dig more into getting a test together, but we should backport
this fix in the mean time.

Refs: #14423
Fixes: #15374
MylesBorins added a commit that referenced this pull request Sep 14, 2017
This error code originally landed in a semver-major commit and is used
by the ESM implementation. This backport includes the error message
and the documentation for the error.

I did attempt to write a test for this, but it did not seem possible
to catch an exception during import, I was also unable to execute
`node --experimental-modules` properly inside of a child_process.

I'll dig more into getting a test together, but we should backport
this fix in the mean time.

Refs: #14423
Fixes: #15374

PR-URL: #15388
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: James M Snell <jasnell@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. http Issues or PRs related to the http subsystem. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants