Skip to content

Commit

Permalink
Fix can't listener error event on incorrect file/directory (#123)
Browse files Browse the repository at this point in the history
* Fix can't listener error event on incorrect file/directory

Co-authored-by: lei2jun <leijun326@gmail.com>
  • Loading branch information
leijuns and lei2jun authored Feb 25, 2022
1 parent 87f687a commit e226156
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,11 @@ function watch(fpath, options, fn) {
}

if (!is.array(fpath) && !is.exists(fpath)) {
watcher.emit('error',
new Error(fpath + ' does not exist.')
);
process.nextTick(function() {
watcher.emit('error',
new Error(fpath + ' does not exist.')
);
});
}

if (is.string(options)) {
Expand Down
11 changes: 6 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,12 @@ describe('options', function() {
describe('parameters', function() {
it('should throw error on non-existed file', function(done) {
var somedir = tree.getPath('home/somedir');
try {
watcher = watch(somedir);
} catch(err) {
done();
}
watcher = watch(somedir);
watcher.on('error', function(err) {
if (err.message.includes('does not exist')) {
done()
}
})
});

it('should accept filename as Buffer', function(done) {
Expand Down

0 comments on commit e226156

Please sign in to comment.