Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

fs.watch error message includes filename #25542

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ function FSWatcher() {
this._handle.onchange = function(status, event, filename) {
if (status < 0) {
self._handle.close();
self.emit('error', errnoException(status, 'watch'));
self.emit('error', errnoException(status, 'watch ' + filename));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of tacking the filename on here, it might be more useful to create the error object, then attach the filename to the error object.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do mean setting a filename property on the error? Looks like I could also just add an error message as the third parameter to errnoException

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant do something like:

var err = errnoException(status, 'watch');
err.filename = filename;

I personally find it more useful to have the filename separate. Maybe others feel differently. Maybe do both.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer both as I want an uncaught error to still display the filename in the message

} else {
self.emit('change', event, filename);
}
Expand All @@ -1175,7 +1175,7 @@ FSWatcher.prototype.start = function(filename, persistent, recursive) {
recursive);
if (err) {
this._handle.close();
throw errnoException(err, 'watch');
throw errnoException(err, 'watch ' + filename);
}
};

Expand Down
30 changes: 30 additions & 0 deletions test/simple/test-fs-watch-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var assert = require('assert');
var fs = require('fs');

try {
fs.watch('non-existant-file')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a semicolon.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think existant should be existent

} catch (error) {
assert(error);
assert(/non-existant-file/.test(error));
}