Skip to content

Commit

Permalink
fs: migrate errors to internal/errors
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
styfle authored and joyeecheung committed Dec 23, 2017
1 parent ae2bed9 commit 8599465
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2554,8 +2554,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)) {
const err = new errors.TypeError('ERR_INVALID_ARG_TYPE',
'data',
'Buffer',
data);
return this.emit('error', err);
}

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

0 comments on commit 8599465

Please sign in to comment.