Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

node.js - Prevent errors from breaking / crashing gulp watch

I'm running gulp 3.6.2 and have the following task that was set up from a sample online

gulp.task('watch', ['default'], function () {
  gulp.watch([
    'views/**/*.html',        
    'public/**/*.js',
    'public/**/*.css'        
  ], function (event) {
    return gulp.src(event.path)
      .pipe(refresh(lrserver));
  });

  gulp.watch(['./app/**/*.coffee'],['scripts']);
  gulp.watch('./app/**/*.scss',['scss']);
});

Any time there's an error in my CoffeeScript gulp watch stops - obviously not what I want.

As recommended elsewhere I tried this

gulp.watch(['./app/**/*.coffee'],['scripts']).on('error', swallowError);
gulp.watch('./app/**/*.scss',['scss']).on('error', swallowError);
function swallowError (error) { error.end(); }

but it doesn't seem to work.

What am I doing wrong?


In response to @Aper?u's answer I modified my swallowError method and tried the following instead:

gulp.task('scripts', function () {
  gulp.src('./app/script/*.coffee')
    .pipe(coffee({ bare: true }))
    .pipe(gulp.dest('./public/js'))
    .on('error', swallowError);
});

Restarted, and then created a syntax error in my coffee file. Same issue:

[gulp] Finished 'scripts' after 306 μs

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: W:ariokartappscriptrishell.coffee:5:1: error: unexpected *
*
^
  at Stream.modifyFile (W:ariokart
ode_modulesgulp-coffeeindex.js:37:33)
  at Stream.stream.write (W:ariokart
ode_modulesgulp-coffee
ode_modulesevent-stream
ode_moduleshroughindex.js:26:11)
  at Stream.ondata (stream.js:51:26)
  at Stream.EventEmitter.emit (events.js:95:17)
  at queueData (W:ariokart
ode_modulesgulp
ode_modulesvinyl-fs
ode_modulesmap-streamindex.js:43:21)
  at next (W:ariokart
ode_modulesgulp
ode_modulesvinyl-fs
ode_modulesmap-streamindex.js:71:7)
  at W:ariokart
ode_modulesgulp
ode_modulesvinyl-fs
ode_modulesmap-streamindex.js:85:7
  at W:ariokart
ode_modulesgulp
ode_modulesvinyl-fslibsrcufferFile.js:8:5
  at fs.js:266:14
  at W:ariokart
ode_modulesgulp
ode_modulesvinyl-fs
ode_modulesgraceful-fsgraceful-fs.js:104:5
  at Object.oncomplete (fs.js:107:15)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your swallowError function should look like this:

function swallowError (error) {

  // If you want details of the error in the console
  console.log(error.toString())

  this.emit('end')
}

I think you have to bind this function on the error event of the task that was falling, not the watch task, because that's not where comes the problem, you should set this error callback on each task that may fail, like plugins that breaks when you have missed a ; or something else, to prevent watch task to stop.

Examples :

gulp.task('all', function () {
  gulp.src('./app/script/*.coffee')
    .pipe(coffee({ bare: true }))
    .on('error', swallowError)
    .pipe(gulp.dest('./public/js'))

  gulp.src('css/*.scss')
    .pipe(sass({ compass: true }))
    .on('error', swallowError)
    .pipe(cssmin())
    .pipe(gulp.dest('dist'))
})

Alternately, if you don't mind to include another module, you can use the log function of gulp-util to keep you from declare an extra function in your gulpfile:

.on('error', gutil.log)

But I may recommend having a look at the awesome gulp-plumber plugin, which is used to remove the onerror handler of the error event, causing the break of the streams. It's very simple to use and it stops you from catch all the tasks that may fail.

gulp.src('./app/script/*.coffee')
  .pipe(plumber())
  .pipe(coffee({ bare: true }))
  .pipe(gulp.dest('./public/js'))

More info about this on this article by the creator of the concerned plugin.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...