-
-
Couldn't load subscription status.
- Fork 33.6k
uv: binding improvements #14933
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
uv: binding improvements #14933
Conversation
The error message when passing in `err >= 0` to `util._errnoException()` was less than useful.
lib/_stream_wrap.js
Outdated
| else | ||
| errCode = uv.UV_EPIPE; | ||
| const code = uv[`UV_${err.code}`]; | ||
| errCode = err.code && code ? code : uv.UV_EPIPE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
teeny tiny suggestion: err.code && code in parentheses might be more readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestions:
- This closes over
err, why not hoist it out of the callback, so onlyerrCodegets closed. - if
!err.codethen`UV_${err.code}` == somethinglike('UV_undefined')souv[`UV_${err.code}`] == false - IIFE can be nice:
const errCode = (() => {
if (!err) return 0;
return uv[`UV_${err.code}`] || uv.UV_EPIPE;
})();|
I don’t think this needs to be |
lib/_stream_wrap.js
Outdated
| else | ||
| errCode = uv.UV_EPIPE; | ||
| const code = uv[`UV_${err.code}`]; | ||
| errCode = err.code && code ? code : uv.UV_EPIPE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestions:
- This closes over
err, why not hoist it out of the callback, so onlyerrCodegets closed. - if
!err.codethen`UV_${err.code}` == somethinglike('UV_undefined')souv[`UV_${err.code}`] == false - IIFE can be nice:
const errCode = (() => {
if (!err) return 0;
return uv[`UV_${err.code}`] || uv.UV_EPIPE;
})();| // cluster modules for stuff like this. | ||
| const errno = uv['UV_' + err.errno]; | ||
| send(errno, null); | ||
| send(uv[`UV_${err.errno}`], null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-0 on this.
I like one expression per line...
test/parallel/test-uv-errno.js
Outdated
| }); | ||
|
|
||
| [0, 1, 'test', {}, [], Infinity, -Infinity, NaN].forEach((key) => { | ||
| assert.throws(() => util._errnoException(key), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the new expectsError syntex:
common.expectsError(
() => util._errnoException(key),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "err" argument must be of type negative number'
}
);| if (key === 'errname') | ||
| return; | ||
|
|
||
| assert.doesNotThrow(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the assert.doesNotThrow is necessary...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not strictly necessary but not a problem either
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it mangles the actual error a little bit:
node/test/parallel/test-assert.js
Lines 485 to 497 in 467385a
| { | |
| let threw = false; | |
| try { | |
| assert.doesNotThrow(makeBlock(thrower, Error)); | |
| } catch (e) { | |
| threw = true; | |
| common.expectsError({ | |
| code: 'ERR_ASSERTION', | |
| message: /Got unwanted exception\.\n\[object Object\]/ | |
| })(e); | |
| } | |
| assert.ok(threw); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm aware of what the function does. These cases do not throw
| const util = require('util'); | ||
| const uv = process.binding('uv'); | ||
|
|
||
| const keys = Object.keys(uv); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'd go:
const keys = Object.keys(uv).filter(k => k !== 'errname');and drop L10-11
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not particularly a fan of using filter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack.
| if (key === 'errname') | ||
| return; // skip this | ||
| const val = uv[key]; | ||
| assert.throws(() => uv[key] = 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT assignment is not a valid lambda expression, you need to () => { uv[key] = 1; }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what am I thinking us 😕? I remember an old debate that ended up with the invention of Object.assign().
Ohh it return the LHS, not the object.
|
Not all comments are blocking, but PTAL. |
|
It has to be semver-major because of the error message change. |
| var e = new Error(message); | ||
| e.code = name; | ||
| e.errno = name; | ||
| const e = new Error(message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO a new type something like errors.UVError or errors.PlatformError would be nice, but then we'd need to expose it somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not something I want to do in this pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
| var name = errname(err); | ||
| if (typeof err !== 'number' || err >= 0 || !Number.isSafeInteger(err)) { | ||
| throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'err', | ||
| 'negative number'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to add the actual argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not particularly useful. The ERR_INVALID_ARG_TYPE actual prints out the type of the actual, not the value itself. So if I passed in _errnoException(1), the error message would be The "err" argument must be of type negative number. Received type number, which is just odd. We could turn this into a RangeError if the err is a number and isn't negative, but that adds a second if-check that is not strictly necessary for what is supposed to be an internal API.
|
CI run was a bit too red, although all of the failures were unrelated. Running again just to be safe: https://ci.nodejs.org/job/node-test-pull-request/9778/ |
* ensure that UV_... props are constants
* improve error message ... the error message when passing
in `err >= 0` to `util._errnoException()` was less than
useful.
* refine uses of process.binding('uv')
PR-URL: #14933
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
|
Landed in 58831b2 |
* ensure that UV_... props are constants
* improve error message ... the error message when passing
in `err >= 0` to `util._errnoException()` was less than
useful.
* refine uses of process.binding('uv')
PR-URL: nodejs/node#14933
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
* ensure that UV_... props are constants
* improve error message ... the error message when passing
in `err >= 0` to `util._errnoException()` was less than
useful.
* refine uses of process.binding('uv')
PR-URL: nodejs/node#14933
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Few improvements to
process.binding('uv')Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
uv