I have followed the recommended solutions found at this question, but I am still seeing errors which break my watchers.
Given a watcher like this:
var through = require('through2');
var watch = require('gulp-watch');
gulp.task('catchall', function() {
return watch('gulpfile.js')
.pipe(through.obj(externalFunc))
.on('error', function(err) {
console.log('Inside error handler:', err);
});
});
With externalFunc defined as:
function externalFunc(file, enc, done) {
throw new Error("I have been compromised - Abort mission!");
}
I would expect to see output of:
[10:52:51] Starting 'catchall'...
[10:52:53] gulpfile.js was changed
Inside error handler: I have been compromised - Abort mission!
Instead, I get no output from externalFunc
, and instead get standard error output and a stacktrace:
[10:52:51] Starting 'catchall'...
[10:52:53] gulpfile.js was changed
/my/path/to/gulpfile.js:27
throw new Error("I have been compromised - Abort mission!");
^
Error: I have been compromised - Abort mission!
at DestroyableTransform.externalFunc [as _transform] ....
Most importantly, the watcher crashes.
Why does this error not get trapped by the on('error')
listener, and what can I do in a gulp watcher to handle these errors without exiting?