fs: improve error message descriptions
authorSakthipriyan Vairamani <thechargingvolcano@gmail.com>
Tue, 2 Jun 2015 15:13:46 +0000 (20:43 +0530)
committerTrevor Norris <trev.norris@gmail.com>
Wed, 10 Jun 2015 22:51:46 +0000 (16:51 -0600)
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>
lib/fs.js
src/fs_event_wrap.cc

index 08b1016..ad44e7b 100644 (file)
--- a/lib/fs.js
+++ b/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);
index 1719942..a6ceff2 100644 (file)
@@ -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]);