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

lib: migrate errors to internal/errors #17719

Closed
wants to merge 9 commits into from
Closed

Conversation

styfle
Copy link
Member

@styfle styfle commented Dec 17, 2017

Refs: #17709

cc @jasnell @joyeecheung

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

lib, test

@nodejs-github-bot nodejs-github-bot added the fs Issues and PRs related to the fs subsystem / file system. label Dec 17, 2017
Copy link
Member

@joyeecheung joyeecheung left a comment

Choose a reason for hiding this comment

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

Hi, thank you for opening the pull request, can you add a test for this new error?

lib/fs.js Outdated
if (!(data instanceof Buffer)) {
const err = new errors.TypeError('ERR_INVALID_ARG_TYPE',
'data',
['string', 'Buffer'],
Copy link
Member

Choose a reason for hiding this comment

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

The type check only checks if the data is a buffer, can you change this to 'Buffer' instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops! I was looking at the usage elsewhere in the same file and missed this. Thanks, I fixed it! 👍

@styfle
Copy link
Member Author

styfle commented Dec 17, 2017

@joyeecheung I created a test but I have no idea if it's in the correct place.
Also it's failing for some reason. 😨
Please advise, thanks! 😄

lib/fs.js Outdated
if (!(data instanceof Buffer)) {
const err = new errors.TypeError('ERR_INVALID_ARG_TYPE',
'data',
['Buffer'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Here can be reduced to use a single string 'Buffer', instead of an array ['Buffer'] : )

}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "data" argument must be of type Buffer without null bytes.' +
Copy link
Contributor

Choose a reason for hiding this comment

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

Umm... If I'm not mistaken, the message here will not match the error it actually throws.
I think it should be 'The "data" argument must be of type Buffer. Received type number'.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's probably why the test is failing, you just need to change this to match the actual error message.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed! 🔧

@@ -41,6 +41,19 @@ const filename = path.join(common.tmpDir, 'sync-write-stream.txt');
assert.strictEqual(fs.readFileSync(filename).equals(chunk), true);
}

// Throws if data is not of type Buffer.
{
const stream = new SyncWriteStream(1);
Copy link
Member

@joyeecheung joyeecheung Dec 18, 2017

Choose a reason for hiding this comment

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

You can just get the stream using new fs.WriteStream, or fs.createWriteStream, SyncWriteStream is being deprecated. That way this test should be in test-fs-write-stream.js instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! I made the changes.

I debugged the file to see why I was getting the error that the code was expected ERR_INVALID_ARG_TYPE but actual was undefined.

It looks like require(fs) is grabbing my installed 8.x fs and not lib/fs.js. Thoughts on this?

@@ -2379,8 +2379,13 @@ WriteStream.prototype.open = function() {


WriteStream.prototype._write = function(data, encoding, cb) {
if (!(data instanceof Buffer))
return this.emit('error', new Error('Invalid data'));
if (!(data instanceof Buffer)) {
Copy link
Member

Choose a reason for hiding this comment

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

Should likely update this to use Buffer.isType() but let's check with @mcollina first

Copy link
Member

Choose a reason for hiding this comment

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

I would keep this as it is, with instanceof. Buffer.isBuffer is mainly needed outside of core.

@@ -2379,8 +2379,13 @@ WriteStream.prototype.open = function() {


WriteStream.prototype._write = function(data, encoding, cb) {
if (!(data instanceof Buffer))
return this.emit('error', new Error('Invalid data'));
if (!(data instanceof Buffer)) {
Copy link
Member

Choose a reason for hiding this comment

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

I would keep this as it is, with instanceof. Buffer.isBuffer is mainly needed outside of core.

'data',
'Buffer',
data);
return this.emit('error', err);
Copy link
Member

Choose a reason for hiding this comment

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

while we are at it, I would see if we can convert this to this.detroy(err) instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @mcollina, this is my first time looking at the source code for Node so I'm not sure what the implications are of changing that line 🙃

  1. Do you want me to change return this.emit('error', err); to return this.detroy(err);?
  2. How would I ensure that change is working correctly (besides existing unit tests)?

Thanks! 😄

Copy link
Member

Choose a reason for hiding this comment

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

  1. yes.

  2. run our unit tests. You can check if _destroy gets called, but that's a plus.

@styfle
Copy link
Member Author

styfle commented Dec 19, 2017

@joyeecheung Can you review again? Thanks!

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

CI: https://ci.nodejs.org/job/node-test-pull-request/12197/

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

@joyeecheung
Copy link
Member

joyeecheung commented Dec 19, 2017

@styfle If you want to run the test with your changes, you can use the node (node.exe on Windows) executable built and linked in the project directory to run the test script. The CI can only be started by a collaborator, whoever runs the CI would usually post the link here.

lib/fs.js Outdated
'data',
'Buffer',
data);
return this.detroy(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

detroy -> destroy

Copy link
Member Author

@styfle styfle Dec 20, 2017

Choose a reason for hiding this comment

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

@starkwang Oops 😬 That’s embarrassing.
I blame @mcollina because I copied the comment above verbatim 😜

In other news, I realized my MacBook wasn’t actually building because Xcode isn’t installed, but linting seems to work fine. 🤨

Let’s see if this one works 💯

@joyeecheung
Copy link
Member

CI: https://ci.nodejs.org/job/node-test-pull-request/12227/

lib/fs.js Outdated
'data',
'Buffer',
data);
return this.destroy(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

The CI seems failed due to this line. It should be:

return this.emit('error', err);

I've tested it on my environment and passed all the test 😉

Copy link
Member Author

Choose a reason for hiding this comment

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

That’s what it was before but @mcollina said to use destroy instead.
I will change it back.
Thanks for the heads up 😎

Copy link
Member Author

Choose a reason for hiding this comment

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

@starkwang How does that look?

@joyeecheung
Copy link
Member

New CI: https://ci.nodejs.org/job/node-test-pull-request/12280/

@styfle
Copy link
Member Author

styfle commented Dec 23, 2017

All, thank you for coaching me through this PR!

I hope everyone takes a break from working on node this week to enjoy time with friends and family! 😄 🎄 🎁 ☃️

@joyeecheung joyeecheung added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Dec 23, 2017
@joyeecheung
Copy link
Member

Landed in 8599465, thanks!

@joyeecheung joyeecheung removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Dec 23, 2017
joyeecheung pushed a commit that referenced this pull request Dec 23, 2017
Throw ERR_INVALID_ARG_TYPE when validating arguments passed to
WriteStream.prototype._write.

Refs: #17709
PR-URL: #17719
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@styfle styfle deleted the fs-error branch December 23, 2017 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. 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.

6 participants