Skip to content

Commit 301f6cc

Browse files
committed
fs: remove watcher state errors for fs.watch
- Remove ERR_FS_WATCHER_ALREADY_STARTED and ERR_FS_WATCHER_NOT_STARTED because those two situations should result in noop instead of errors for consistency with the documented behavior of fs.watchFile. This partially reverts #19089 - Update comments about this behavior. Refs: #19089 PR-URL: #19345 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 897cec4 commit 301f6cc

File tree

4 files changed

+12
-34
lines changed

4 files changed

+12
-34
lines changed

doc/api/errors.md

-12
Original file line numberDiff line numberDiff line change
@@ -783,18 +783,6 @@ falsy value.
783783
An invalid symlink type was passed to the [`fs.symlink()`][] or
784784
[`fs.symlinkSync()`][] methods.
785785

786-
<a id="ERR_FS_WATCHER_ALREADY_STARTED"></a>
787-
### ERR_FS_WATCHER_ALREADY_STARTED
788-
789-
An attempt was made to start a watcher returned by `fs.watch()` that has
790-
already been started.
791-
792-
<a id="ERR_FS_WATCHER_NOT_STARTED"></a>
793-
### ERR_FS_WATCHER_NOT_STARTED
794-
795-
An attempt was made to initiate operations on a watcher returned by
796-
`fs.watch()` that has not yet been started.
797-
798786
<a id="ERR_HTTP_HEADERS_SENT"></a>
799787
### ERR_HTTP_HEADERS_SENT
800788

lib/fs.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ const fs = exports;
3636
const { Buffer } = require('buffer');
3737
const errors = require('internal/errors');
3838
const {
39-
ERR_FS_WATCHER_ALREADY_STARTED,
40-
ERR_FS_WATCHER_NOT_STARTED,
4139
ERR_INVALID_ARG_TYPE,
4240
ERR_INVALID_CALLBACK,
4341
ERR_OUT_OF_RANGE
@@ -1342,25 +1340,26 @@ util.inherits(FSWatcher, EventEmitter);
13421340

13431341
// FIXME(joyeecheung): this method is not documented.
13441342
// At the moment if filename is undefined, we
1345-
// 1. Throw an Error from C++ land if it's the first time .start() is called
1346-
// 2. Return silently from C++ land if .start() has already been called
1343+
// 1. Throw an Error if it's the first time .start() is called
1344+
// 2. Return silently if .start() has already been called
13471345
// on a valid filename and the wrap has been initialized
1346+
// This method is a noop if the watcher has already been started.
13481347
FSWatcher.prototype.start = function(filename,
13491348
persistent,
13501349
recursive,
13511350
encoding) {
13521351
lazyAssert()(this._handle instanceof FSEvent, 'handle must be a FSEvent');
13531352
if (this._handle.initialized) {
1354-
throw new ERR_FS_WATCHER_ALREADY_STARTED();
1353+
return;
13551354
}
13561355

13571356
filename = getPathFromURL(filename);
13581357
validatePath(filename, 'filename');
13591358

1360-
var err = this._handle.start(pathModule.toNamespacedPath(filename),
1361-
persistent,
1362-
recursive,
1363-
encoding);
1359+
const err = this._handle.start(pathModule.toNamespacedPath(filename),
1360+
persistent,
1361+
recursive,
1362+
encoding);
13641363
if (err) {
13651364
const error = errors.uvException({
13661365
errno: err,
@@ -1372,10 +1371,11 @@ FSWatcher.prototype.start = function(filename,
13721371
}
13731372
};
13741373

1374+
// This method is a noop if the watcher has not been started.
13751375
FSWatcher.prototype.close = function() {
13761376
lazyAssert()(this._handle instanceof FSEvent, 'handle must be a FSEvent');
13771377
if (!this._handle.initialized) {
1378-
throw new ERR_FS_WATCHER_NOT_STARTED();
1378+
return;
13791379
}
13801380
this._handle.close();
13811381
};

lib/internal/errors.js

-4
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,6 @@ E('ERR_FALSY_VALUE_REJECTION', 'Promise was rejected with falsy value', Error);
654654
E('ERR_FS_INVALID_SYMLINK_TYPE',
655655
'Symlink type must be one of "dir", "file", or "junction". Received "%s"',
656656
Error); // Switch to TypeError. The current implementation does not seem right
657-
E('ERR_FS_WATCHER_ALREADY_STARTED',
658-
'The watcher has already been started', Error);
659-
E('ERR_FS_WATCHER_NOT_STARTED',
660-
'The watcher has not been started', Error);
661657
E('ERR_HTTP2_ALTSVC_INVALID_ORIGIN',
662658
'HTTP/2 ALTSVC frames require a valid origin', TypeError);
663659
E('ERR_HTTP2_ALTSVC_LENGTH',

test/parallel/test-fs-watch.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,10 @@ for (const testCase of cases) {
6565
assert.strictEqual(eventType, 'change');
6666
assert.strictEqual(argFilename, testCase.fileName);
6767

68-
common.expectsError(() => watcher.start(), {
69-
code: 'ERR_FS_WATCHER_ALREADY_STARTED',
70-
message: 'The watcher has already been started'
71-
});
68+
watcher.start(); // starting a started watcher should be a noop
7269
// end of test case
7370
watcher.close();
74-
common.expectsError(() => watcher.close(), {
75-
code: 'ERR_FS_WATCHER_NOT_STARTED',
76-
message: 'The watcher has not been started'
77-
});
71+
watcher.close(); // closing a closed watcher should be a noop
7872
}));
7973

8074
// long content so it's actually flushed. toUpperCase so there's real change.

0 commit comments

Comments
 (0)