Skip to content

Commit 3f691b6

Browse files
author
ronkorving
committed
fs: fix the error message when file open flags are a bad type
Before, it would show "TypeError: flags must be an int", which in native Node is technically correct. But in JS they may be (and usually are) strings. Fixes #2871
1 parent 845acb4 commit 3f691b6

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/fs.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,16 @@ fs.readFileSync = function(path, options) {
465465

466466
// Used by binding.open and friends
467467
function stringToFlags(flag) {
468-
// Only mess with strings
469-
if (typeof flag !== 'string') {
468+
// Integers are already good to go
469+
if (Number.isInteger(flag)) {
470470
return flag;
471471
}
472472

473+
// We only allow strings from here on
474+
if (typeof flag !== 'string') {
475+
throw new TypeError('File open flags may only be string or integer');
476+
}
477+
473478
switch (flag) {
474479
case 'r' : return O_RDONLY;
475480
case 'rs' : // fall through

test/parallel/test-fs-open.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ fs.open(__filename, 'rs', function(err, fd) {
3232
openFd2 = fd;
3333
});
3434

35+
assert.throws(function() {
36+
fs.open('/path/to/file/that/does/not/exist', { bad: 'flags' }, assert.fail);
37+
}, function(err) {
38+
if (err instanceof TypeError) {
39+
return err.message === 'File open flags may only be string or integer';
40+
}
41+
});
42+
3543
process.on('exit', function() {
3644
assert.ok(openFd);
3745
assert.ok(openFd2);
3846
});
39-

0 commit comments

Comments
 (0)