Skip to content

fs: improve error message for invalid flag #5590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-fs-open-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/
);