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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

should this be done for each tasks ? If i have n number of functions, is there any way handle all of them together ??
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.