@@ -35,6 +35,10 @@ const isWindows = process.platform === 'win32';
35
35
const DEBUG = process . env . NODE_DEBUG && / f s / . test ( process . env . NODE_DEBUG ) ;
36
36
const errnoException = util . _errnoException ;
37
37
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
+ }
38
42
39
43
function rethrow ( ) {
40
44
// Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
@@ -226,12 +230,13 @@ fs.existsSync = function(path) {
226
230
fs . readFile = function ( path , options , callback_ ) {
227
231
var callback = maybeCallback ( arguments [ arguments . length - 1 ] ) ;
228
232
229
- if ( ! options || typeof options === 'function' )
233
+ if ( ! options || typeof options === 'function' ) {
230
234
options = { encoding : null , flag : 'r' } ;
231
- else if ( typeof options === 'string' )
235
+ } else if ( typeof options === 'string' ) {
232
236
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
+ }
235
240
236
241
var encoding = options . encoding ;
237
242
assertEncoding ( encoding ) ;
@@ -389,7 +394,7 @@ fs.readFileSync = function(path, options) {
389
394
} else if ( typeof options === 'string' ) {
390
395
options = { encoding : options , flag : 'r' } ;
391
396
} else if ( typeof options !== 'object' ) {
392
- throw new TypeError ( 'Bad arguments' ) ;
397
+ throwOptionsError ( options ) ;
393
398
}
394
399
395
400
var encoding = options . encoding ;
@@ -1119,7 +1124,7 @@ fs.writeFile = function(path, data, options, callback) {
1119
1124
} else if ( typeof options === 'string' ) {
1120
1125
options = { encoding : options , mode : 0o666 , flag : 'w' } ;
1121
1126
} else if ( typeof options !== 'object' ) {
1122
- throw new TypeError ( 'Bad arguments' ) ;
1127
+ throwOptionsError ( options ) ;
1123
1128
}
1124
1129
1125
1130
assertEncoding ( options . encoding ) ;
@@ -1143,7 +1148,7 @@ fs.writeFileSync = function(path, data, options) {
1143
1148
} else if ( typeof options === 'string' ) {
1144
1149
options = { encoding : options , mode : 0o666 , flag : 'w' } ;
1145
1150
} else if ( typeof options !== 'object' ) {
1146
- throw new TypeError ( 'Bad arguments' ) ;
1151
+ throwOptionsError ( options ) ;
1147
1152
}
1148
1153
1149
1154
assertEncoding ( options . encoding ) ;
@@ -1178,7 +1183,7 @@ fs.appendFile = function(path, data, options, callback_) {
1178
1183
} else if ( typeof options === 'string' ) {
1179
1184
options = { encoding : options , mode : 0o666 , flag : 'a' } ;
1180
1185
} else if ( typeof options !== 'object' ) {
1181
- throw new TypeError ( 'Bad arguments' ) ;
1186
+ throwOptionsError ( options ) ;
1182
1187
}
1183
1188
1184
1189
if ( ! options . flag )
@@ -1192,7 +1197,7 @@ fs.appendFileSync = function(path, data, options) {
1192
1197
} else if ( typeof options === 'string' ) {
1193
1198
options = { encoding : options , mode : 0o666 , flag : 'a' } ;
1194
1199
} else if ( typeof options !== 'object' ) {
1195
- throw new TypeError ( 'Bad arguments' ) ;
1200
+ throwOptionsError ( options ) ;
1196
1201
}
1197
1202
if ( ! options . flag )
1198
1203
options = util . _extend ( { flag : 'a' } , options ) ;
0 commit comments