fs: migrate errors to internal/errors

Throw ERR_INVALID_ARG_TYPE when validating arguments passed to
WriteStream.prototype._write.

Refs: https://github.com/nodejs/node/issues/17709
PR-URL: https://github.com/nodejs/node/pull/17719
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Steven 2017-12-17 14:55:22 -05:00 committed by Joyee Cheung
parent ae2bed9938
commit 8599465d33
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
2 changed files with 20 additions and 2 deletions

View File

@ -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() {

View File

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