Skip to content

Commit

Permalink
fs: include filename in watch errors
Browse files Browse the repository at this point in the history
This commit adds the relevant filename to fs.watch() errors.

Refs: nodejs/node-v0.x-archive#25542
PR-URL: #2748
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
charlierudolph authored and jasnell committed Oct 8, 2015
1 parent 7271cb0 commit 4a35ba4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,9 @@ function FSWatcher() {
this._handle.onchange = function(status, event, filename) {
if (status < 0) {
self._handle.close();
self.emit('error', errnoException(status, 'watch'));
const error = errnoException(status, `watch ${filename}`);
error.filename = filename;
self.emit('error', error);
} else {
self.emit('change', event, filename);
}
Expand All @@ -1234,7 +1236,9 @@ FSWatcher.prototype.start = function(filename, persistent, recursive) {
recursive);
if (err) {
this._handle.close();
throw errnoException(err, 'watch');
const error = errnoException(err, `watch ${filename}`);
error.filename = filename;
throw error;
}
};

Expand Down
17 changes: 17 additions & 0 deletions test/sequential/test-fs-watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,20 @@ assert.throws(function() {
w.stop();
}, TypeError);
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');

0 comments on commit 4a35ba4

Please sign in to comment.