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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

const err = new errors.TypeError('ERR_INVALID_ARG_TYPE',
'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.

}

if (typeof this.fd !== 'number') {
return this.once('open', function() {
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-fs-write-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,16 @@ common.refreshTmpDir();
});
stream.destroy();
}

// Throws if data is not of type Buffer.
{
const stream = fs.createWriteStream(file);
common.expectsError(() => {
stream._write(42, null, function() {});
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "data" argument must be of type Buffer. Received type number'
});
stream.destroy();
}