2

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.

Vikhyath Maiya
  • 3,122
  • 3
  • 34
  • 68

1 Answers1

1

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

Nidhi Agarwal
  • 316
  • 2
  • 13
  • should this be done for each tasks ? If i have n number of functions, is there any way handle all of them together ?? – Vikhyath Maiya Dec 11 '17 at 09:39
  • Yes you can but by the time you catch the error and execute callback it will start another task. This was happening when i was trying to do it. because before callback i had an async call which was taking time. – Nidhi Agarwal Dec 11 '17 at 10:04