From a255da40d1de13c918223b4c814d79376ced0ccb Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 7 Mar 2016 12:19:48 -0800 Subject: [PATCH 1/2] fs: improve error message for invalid flag Flags on fs.open and others can be passed as strings or int. Previously, if passing anything other than string or int, the error message would only say that flags must be an int. --- lib/fs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 1fd40b9f4fcada..9aa4eaa6a562ec 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -536,8 +536,8 @@ fs.readFileSync = function(path, options) { // Used by binding.open and friends function stringToFlags(flag) { - // Only mess with strings - if (typeof flag !== 'string') { + // Return early if it's a number + if (typeof flag === 'number') { return flag; } From 45ee0497d9134ee56c22fcafa398c301d5270d56 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 7 Mar 2016 12:32:54 -0800 Subject: [PATCH 2/2] test: expand test case for unknown file open flags --- test/parallel/test-fs-open-flags.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/parallel/test-fs-open-flags.js b/test/parallel/test-fs-open-flags.js index 16b3d35cae5a49..cf451b4f8d5d95 100644 --- a/test/parallel/test-fs-open-flags.js +++ b/test/parallel/test-fs-open-flags.js @@ -34,3 +34,18 @@ assert.equal(fs._stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL); .forEach(function(flags) { assert.throws(function() { fs._stringToFlags(flags); }); }); + +assert.throws( + () => fs._stringToFlags({}), + /Unknown file open flag: \[object Object\]/ +); + +assert.throws( + () => fs._stringToFlags(true), + /Unknown file open flag: true/ +); + +assert.throws( + () => fs._stringToFlags(null), + /Unknown file open flag: null/ +);