Browse Source

fs: improve error message descriptions

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: https://github.com/nodejs/io.js/pull/1870
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
v2.3.1-release
Sakthipriyan Vairamani 10 years ago
committed by Trevor Norris
parent
commit
09f2a67bd8
  1. 23
      lib/fs.js
  2. 2
      src/fs_event_wrap.cc

23
lib/fs.js

@ -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
@ -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);
@ -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;
@ -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);
@ -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);
@ -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)
@ -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);

2
src/fs_event_wrap.cc

@ -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]);

Loading…
Cancel
Save