From 09f2a67bd82faf67229dd7e9a9c45e3f1b3f6c17 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Tue, 2 Jun 2015 20:43:46 +0530 Subject: [PATCH] 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 Reviewed-By: Trevor Norris --- lib/fs.js | 23 ++++++++++++++--------- src/fs_event_wrap.cc | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 08b101625faf53..ad44e7b30c6771 100644 --- 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); diff --git a/src/fs_event_wrap.cc b/src/fs_event_wrap.cc index 1719942ce3e099..a6ceff2776db92 100644 --- a/src/fs_event_wrap.cc +++ b/src/fs_event_wrap.cc @@ -86,7 +86,7 @@ void FSEventWrap::Start(const FunctionCallbackInfo& args) { FSEventWrap* wrap = Unwrap(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]);