fs: include filename in watch errors

This commit adds the relevant filename to fs.watch() errors.

Refs: https://github.com/nodejs/node-v0.x-archive/pull/25542
PR-URL: https://github.com/nodejs/node/pull/2748
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
charlierudolph 2015-09-29 22:03:27 -07:00 committed by cjihrig
parent 02fe8215f0
commit 87e820ead5
2 changed files with 23 additions and 2 deletions

View File

@ -1219,7 +1219,9 @@ function FSWatcher() {
this._handle.onchange = function(status, event, filename) { this._handle.onchange = function(status, event, filename) {
if (status < 0) { if (status < 0) {
self._handle.close(); self._handle.close();
self.emit('error', errnoException(status, 'watch')); const error = errnoException(status, `watch ${filename}`);
error.filename = filename;
self.emit('error', error);
} else { } else {
self.emit('change', event, filename); self.emit('change', event, filename);
} }
@ -1234,7 +1236,9 @@ FSWatcher.prototype.start = function(filename, persistent, recursive) {
recursive); recursive);
if (err) { if (err) {
this._handle.close(); this._handle.close();
throw errnoException(err, 'watch'); const error = errnoException(err, `watch ${filename}`);
error.filename = filename;
throw error;
} }
}; };

View File

@ -126,3 +126,20 @@ assert.throws(function() {
w.stop(); w.stop();
}, TypeError); }, TypeError);
oldhandle.stop(); // clean up oldhandle.stop(); // clean up
assert.throws(function() {
fs.watch('non-existent-file');
}, function(err) {
assert(err);
assert(/non-existent-file/.test(err));
assert.equal(err.filename, 'non-existent-file');
return true;
});
var watcher = fs.watch(__filename);
watcher.on('error', common.mustCall(function(err) {
assert(err);
assert(/non-existent-file/.test(err));
assert.equal(err.filename, 'non-existent-file');
}));
watcher._handle.onchange(-1, 'ENOENT', 'non-existent-file');