Skip to content

Commit

Permalink
fs: improve error message descriptions
Browse files Browse the repository at this point in the history
1. Change "Bad arguments" error messages to a more helpful message
"options should either be an object or a string".

2. Make braces consistent.

3. Return meaningful error message from fs_event_wrap's
FSEvent's Start function.

PR-URL: #1870
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
thefourtheye authored and trevnorris committed Jun 10, 2015
1 parent cf5020f commit 09f2a67
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const isWindows = process.platform === 'win32';
const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
const errnoException = util._errnoException;

function throwOptionsError(options) {
throw new TypeError('Expected options to be either an object or a string, ' +
'but got ' + typeof options + ' instead');
}

function rethrow() {
// Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
Expand Down Expand Up @@ -226,12 +230,13 @@ fs.existsSync = function(path) {
fs.readFile = function(path, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);

if (!options || typeof options === 'function')
if (!options || typeof options === 'function') {
options = { encoding: null, flag: 'r' };
else if (typeof options === 'string')
} else if (typeof options === 'string') {
options = { encoding: options, flag: 'r' };
else if (typeof options !== 'object')
throw new TypeError('Bad arguments');
} else if (typeof options !== 'object') {
throwOptionsError(options);
}

var encoding = options.encoding;
assertEncoding(encoding);
Expand Down Expand Up @@ -389,7 +394,7 @@ fs.readFileSync = function(path, options) {
} else if (typeof options === 'string') {
options = { encoding: options, flag: 'r' };
} else if (typeof options !== 'object') {
throw new TypeError('Bad arguments');
throwOptionsError(options);
}

var encoding = options.encoding;
Expand Down Expand Up @@ -1119,7 +1124,7 @@ fs.writeFile = function(path, data, options, callback) {
} else if (typeof options === 'string') {
options = { encoding: options, mode: 0o666, flag: 'w' };
} else if (typeof options !== 'object') {
throw new TypeError('Bad arguments');
throwOptionsError(options);
}

assertEncoding(options.encoding);
Expand All @@ -1143,7 +1148,7 @@ fs.writeFileSync = function(path, data, options) {
} else if (typeof options === 'string') {
options = { encoding: options, mode: 0o666, flag: 'w' };
} else if (typeof options !== 'object') {
throw new TypeError('Bad arguments');
throwOptionsError(options);
}

assertEncoding(options.encoding);
Expand Down Expand Up @@ -1178,7 +1183,7 @@ fs.appendFile = function(path, data, options, callback_) {
} else if (typeof options === 'string') {
options = { encoding: options, mode: 0o666, flag: 'a' };
} else if (typeof options !== 'object') {
throw new TypeError('Bad arguments');
throwOptionsError(options);
}

if (!options.flag)
Expand All @@ -1192,7 +1197,7 @@ fs.appendFileSync = function(path, data, options) {
} else if (typeof options === 'string') {
options = { encoding: options, mode: 0o666, flag: 'a' };
} else if (typeof options !== 'object') {
throw new TypeError('Bad arguments');
throwOptionsError(options);
}
if (!options.flag)
options = util._extend({ flag: 'a' }, options);
Expand Down
2 changes: 1 addition & 1 deletion src/fs_event_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.Holder());

if (args.Length() < 1 || !args[0]->IsString()) {
return env->ThrowTypeError("Bad arguments");
return env->ThrowTypeError("filename must be a valid string");
}

node::Utf8Value path(env->isolate(), args[0]);
Expand Down

0 comments on commit 09f2a67

Please sign in to comment.