Skip to content

Commit 8599465

Browse files
styflejoyeecheung
authored andcommitted
fs: migrate errors to internal/errors
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>
1 parent ae2bed9 commit 8599465

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/fs.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,8 +2554,13 @@ WriteStream.prototype.open = function() {
25542554

25552555

25562556
WriteStream.prototype._write = function(data, encoding, cb) {
2557-
if (!(data instanceof Buffer))
2558-
return this.emit('error', new Error('Invalid data'));
2557+
if (!(data instanceof Buffer)) {
2558+
const err = new errors.TypeError('ERR_INVALID_ARG_TYPE',
2559+
'data',
2560+
'Buffer',
2561+
data);
2562+
return this.emit('error', err);
2563+
}
25592564

25602565
if (typeof this.fd !== 'number') {
25612566
return this.once('open', function() {

test/parallel/test-fs-write-stream.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,16 @@ common.refreshTmpDir();
4949
});
5050
stream.destroy();
5151
}
52+
53+
// Throws if data is not of type Buffer.
54+
{
55+
const stream = fs.createWriteStream(file);
56+
common.expectsError(() => {
57+
stream._write(42, null, function() {});
58+
}, {
59+
code: 'ERR_INVALID_ARG_TYPE',
60+
type: TypeError,
61+
message: 'The "data" argument must be of type Buffer. Received type number'
62+
});
63+
stream.destroy();
64+
}

0 commit comments

Comments
 (0)