From 3f691b6bcdd911853adf72be3dc5eabcd9fa57d8 Mon Sep 17 00:00:00 2001 From: ronkorving Date: Tue, 15 Sep 2015 19:02:37 +0900 Subject: [PATCH] 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 --- lib/fs.js | 9 +++++++-- test/parallel/test-fs-open.js | 9 ++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index eeb3806cdc8861..dbfdf8f14eff13 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -465,11 +465,16 @@ fs.readFileSync = function(path, options) { // Used by binding.open and friends function stringToFlags(flag) { - // Only mess with strings - if (typeof flag !== 'string') { + // Integers are already good to go + if (Number.isInteger(flag)) { return flag; } + // We only allow strings from here on + if (typeof flag !== 'string') { + throw new TypeError('File open flags may only be string or integer'); + } + switch (flag) { case 'r' : return O_RDONLY; case 'rs' : // fall through diff --git a/test/parallel/test-fs-open.js b/test/parallel/test-fs-open.js index 59b605841b4440..82748bed73e3b8 100644 --- a/test/parallel/test-fs-open.js +++ b/test/parallel/test-fs-open.js @@ -32,8 +32,15 @@ fs.open(__filename, 'rs', function(err, fd) { openFd2 = fd; }); +assert.throws(function() { + fs.open('/path/to/file/that/does/not/exist', { bad: 'flags' }, assert.fail); +}, function(err) { + if (err instanceof TypeError) { + return err.message === 'File open flags may only be string or integer'; + } +}); + process.on('exit', function() { assert.ok(openFd); assert.ok(openFd2); }); -