Skip to content

Commit 09f2a67

Browse files
thefourtheyetrevnorris
authored andcommitted
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: #1870 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent cf5020f commit 09f2a67

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/fs.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const isWindows = process.platform === 'win32';
3535
const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
3636
const errnoException = util._errnoException;
3737

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

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

229-
if (!options || typeof options === 'function')
233+
if (!options || typeof options === 'function') {
230234
options = { encoding: null, flag: 'r' };
231-
else if (typeof options === 'string')
235+
} else if (typeof options === 'string') {
232236
options = { encoding: options, flag: 'r' };
233-
else if (typeof options !== 'object')
234-
throw new TypeError('Bad arguments');
237+
} else if (typeof options !== 'object') {
238+
throwOptionsError(options);
239+
}
235240

236241
var encoding = options.encoding;
237242
assertEncoding(encoding);
@@ -389,7 +394,7 @@ fs.readFileSync = function(path, options) {
389394
} else if (typeof options === 'string') {
390395
options = { encoding: options, flag: 'r' };
391396
} else if (typeof options !== 'object') {
392-
throw new TypeError('Bad arguments');
397+
throwOptionsError(options);
393398
}
394399

395400
var encoding = options.encoding;
@@ -1119,7 +1124,7 @@ fs.writeFile = function(path, data, options, callback) {
11191124
} else if (typeof options === 'string') {
11201125
options = { encoding: options, mode: 0o666, flag: 'w' };
11211126
} else if (typeof options !== 'object') {
1122-
throw new TypeError('Bad arguments');
1127+
throwOptionsError(options);
11231128
}
11241129

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

11491154
assertEncoding(options.encoding);
@@ -1178,7 +1183,7 @@ fs.appendFile = function(path, data, options, callback_) {
11781183
} else if (typeof options === 'string') {
11791184
options = { encoding: options, mode: 0o666, flag: 'a' };
11801185
} else if (typeof options !== 'object') {
1181-
throw new TypeError('Bad arguments');
1186+
throwOptionsError(options);
11821187
}
11831188

11841189
if (!options.flag)
@@ -1192,7 +1197,7 @@ fs.appendFileSync = function(path, data, options) {
11921197
} else if (typeof options === 'string') {
11931198
options = { encoding: options, mode: 0o666, flag: 'a' };
11941199
} else if (typeof options !== 'object') {
1195-
throw new TypeError('Bad arguments');
1200+
throwOptionsError(options);
11961201
}
11971202
if (!options.flag)
11981203
options = util._extend({ flag: 'a' }, options);

src/fs_event_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
8686
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.Holder());
8787

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

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

0 commit comments

Comments
 (0)