jprichardson / node-jsonfile

Easily read/write JSON files.
MIT License
1.2k stars 321 forks source link

Error callback when null filename sent in jsonfile.readfile #77

Closed giper45 closed 7 years ago

giper45 commented 7 years ago

Using jsonfile in my project, I found that in read file if you try to send a null filename it doesn't return an error but throw an exception that exit from app. More precisely it is an fs issue / choice, anyway it wouldn't better if jsonfile returns an error from callback if null input filename is sent?
Here the test :

  describe('> when null JSON file name sent in read', function () {
    it('should send an error if null is sent on filename', function (done) {
      jf.readFile(null, function (err, obj2) {
        assert(err instanceof Error)
        done()
      })  
    })  
  }) 

A solution could be the following:

 function readFile (file, options, callback) {
+  file = file || ''
   if (callback == null) {
     callback = options
     options = {}
   ....

Simply add file = file || '' when readFile starts
Thanks !

RyanZim commented 7 years ago

I don't see the point of this, since this is how the native fs.readFile() method works. The reasoning is that an incorrect type is a programmer error, not a app runtime error.

See https://runkit.com/57ebc041d4a27f140028fd78/59022f55acedf10011cce7ae