Skip to content

Commit 87e820e

Browse files
charlierudolphcjihrig
charlierudolph
authored andcommitted
fs: include filename in watch errors
This commit adds the relevant filename to fs.watch() errors. Refs: nodejs/node-v0.x-archive#25542 PR-URL: #2748 Reviewed-By: Colin Ihrig <[email protected]>
1 parent 02fe821 commit 87e820e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/fs.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,9 @@ function FSWatcher() {
12191219
this._handle.onchange = function(status, event, filename) {
12201220
if (status < 0) {
12211221
self._handle.close();
1222-
self.emit('error', errnoException(status, 'watch'));
1222+
const error = errnoException(status, `watch ${filename}`);
1223+
error.filename = filename;
1224+
self.emit('error', error);
12231225
} else {
12241226
self.emit('change', event, filename);
12251227
}
@@ -1234,7 +1236,9 @@ FSWatcher.prototype.start = function(filename, persistent, recursive) {
12341236
recursive);
12351237
if (err) {
12361238
this._handle.close();
1237-
throw errnoException(err, 'watch');
1239+
const error = errnoException(err, `watch ${filename}`);
1240+
error.filename = filename;
1241+
throw error;
12381242
}
12391243
};
12401244

test/sequential/test-fs-watch.js

+17
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,20 @@ assert.throws(function() {
126126
w.stop();
127127
}, TypeError);
128128
oldhandle.stop(); // clean up
129+
130+
assert.throws(function() {
131+
fs.watch('non-existent-file');
132+
}, function(err) {
133+
assert(err);
134+
assert(/non-existent-file/.test(err));
135+
assert.equal(err.filename, 'non-existent-file');
136+
return true;
137+
});
138+
139+
var watcher = fs.watch(__filename);
140+
watcher.on('error', common.mustCall(function(err) {
141+
assert(err);
142+
assert(/non-existent-file/.test(err));
143+
assert.equal(err.filename, 'non-existent-file');
144+
}));
145+
watcher._handle.onchange(-1, 'ENOENT', 'non-existent-file');

0 commit comments

Comments
 (0)