Search code examples
gulpgulp-watchgulp-sassgulp-uglify

gulp stopping gulp process on any error


i am using gulp in my project for prod build related activities. I have tasks like following

gulp.task('bundle-libs', bundlelibs);
gulp.task('bundle-modules', bundle);

/* build libs for production */
gulp.task('build:prod', function (done) {
    runSequence('clean', ['styles', 'copy-assets'], 'compile', 'systemjs_config', 'prefix_routes', 'bundle-libs', done)
});

/* create module bundles */
gulp.task('bundle', function (done) {
    runSequence('copy-required-files', 'bundle-modules', done);
}); 

But in this , when any one of the tasks fail i get an error in the console, but the build process continues. What i want to do is , when any of the tasks fail, i want to show suitable error in the console and stop the build process ie i do not want to execute the subsequent tasks. How to go about this ? i searched online, but couldnt get anything that could solve the issue. Please help. Thank you.


Solution

  • It depends on the type of error you are trying to handle. It it was thrown as an exception you can handle it using try-catch. It will go in catch block and there don't execute done function if you don't want to execute remaining tasks. example:

    function testTask(done) {
      try {
        return gulp.src('tmp/dist/*.html')
              .pipe(gulp.dest('tmp/dist'));
      } catch(error) {
        const error_message = 'Error:: task failed. ' + error.name + ' : ' + error.message;
      }
    }
    

    If it is error callback, there are many ways to handle them. You can try gulp plumber