Skip to content

Commit

Permalink
Merge pull request #2 from faucet-pipeline/errors
Browse files Browse the repository at this point in the history
Handle errors
  • Loading branch information
moonglum authored Nov 18, 2017
2 parents 82cfa0d + 3b73f49 commit f408178
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
/package-lock.json
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ ourselves. We simply use [chokidar](https://github.com/paulmillr/chokidar).
Using nite-owl basically comes down to this:

```js
let watch = require('nite-owl')
let watch = require("nite-owl");

watch(myFavoriteDirectory)
.on('edit', myFavoriteFunction)
.on("edit", myFavoriteFunction)
.on("error", myErrorFunction);
```

Now, whenever something about the files in `myFavoriteDirectory` changes, the
Expand All @@ -24,6 +25,23 @@ This notification is debounced: You only get notified at most once every 50
milliseconds. You can adjust that value by providing a second argument to the
watch function.

The error callback will be called, when watching the files resulted in an
error. The most common error is the `TooManyFilesError` (it has the code
`ERR_TOO_MANY_FILES`). It occurs on Linux when you watch too many files. In this
case you have to either increase the inotify limits or choose to watch less
files. An error handler could look like this:

```js
watch(myFavoriteDirectory).on("error", err => {
if(err.code === "ERR_TOO_MANY_FILES") {
console.error("Watching too many files");
process.exit(1);
} else {
throw err;
}
});
```

## License

Licensed under Apache 2.0.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ let chokidar = require("chokidar");
let EventEmitter = require("events");
let path = require("path");

class TooManyFilesError extends Error {
constructor(...params) {
super(...params);
this.code = "ERR_TOO_MANY_FILES";
}
};

module.exports = (rootDirs, delay = 50) => {
if(!rootDirs.pop) {
rootDirs = [rootDirs];
Expand All @@ -21,6 +28,17 @@ module.exports = (rootDirs, delay = 50) => {
watcher.on("add", notify).
on("change", notify).
on("unlink", notify);
}).on("error", err => {
if(err.code === "ENOSPC") {
err = new TooManyFilesError("You are watching too many files");
}

// Emit the error if someone is listening. Otherwise throw it.
if(emitter.listenerCount("error") > 0) {
emitter.emit("error", err);
} else {
throw err;
}
});

return emitter;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nite-owl",
"version": "3.1.0",
"version": "3.2.0",
"description": "A debounced EventEmitter that watches for file changes",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f408178

Please sign in to comment.