joyent/node — pull request #3918 by bjouhier (2012-08-24 11:48:30+00:00)
Title: fs.readFile did not catch encoding error

If you passed an invalid encoding to `fs.readFile` the exception was not caught and not propagated to the callback. Instead the process would die with an unhandled exception.

Repro:

``` javascript
require('fs').readFile(__filename, "utf9", function(err, result) {
console.log('err=' + err + ', result=' + result);
});
```

Output:

```
buffer.js:434
throw new Error('Unknown encoding');
^
Error: Unknown encoding
at Buffer.toString (buffer.js:434:13)
at fs.readFile (fs.js:175:37)
at Object.oncomplete (fs.js:297:15)
```

instead of:

```
err=Error: Unknown encoding, result=undefined
```

Notes:
* I tested for `encoding && er` because I don't want to clobber previous error. It would probably be better to test for `er` at the beginning of the `close` function.
* I added a test file and tried to follow the style of other test files. Don't know if this is what you expect, or if I put it in the right place (this is my first pull request).

Thanks @tchambard for spotting the problem.