Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gulp wrap pipe
/**
* Wrap gulp streams into fail-safe function for better error reporting
* Usage:
* gulp.task('less', wrapPipe(function(success, error) {
* return gulp.src('less/*.less')
* .pipe(less().on('error', error))
* .pipe(gulp.dest('app/css'));
* }));
*/
function wrapPipe(taskFn) {
return function(done) {
var onSuccess = function() {
done();
};
var onError = function(err) {
done(err);
}
var outStream = taskFn(onSuccess, onError);
if(outStream && typeof outStream.on === 'function') {
outStream.on('end', onSuccess);
}
}
}
@bondden

This comment has been minimized.

Copy link

@bondden bondden commented Jun 1, 2015

There's a problem with the code while using gulp to run asynchronous mocha tests:
Error: task completion callback called too many times
at line 14 here.

@just-boris

This comment has been minimized.

Copy link
Owner Author

@just-boris just-boris commented Jun 1, 2015

I guess you have a gulp task for mocha like this:

gulp.task('mocha', function() {
    return gulp.src('test/*.spec.js', {read: false})
        .pipe(mocha())
});

Because mocha plugin last in pipe, gulp can catch an error from here by yourself.
You don't need to add use wrapper in that case. This wrapper is made for the catching error in a middle of pipes.

@bondden

This comment has been minimized.

Copy link

@bondden bondden commented Jun 1, 2015

Yes, I also removed wrapper from mocha task.
Thank`s for nice idea to catch build process errors!

@NeXTs

This comment has been minimized.

Copy link

@NeXTs NeXTs commented Jun 2, 2015

Let's assume I have few consecutive actions in one task. I have to catch errors for each of them?
Somehow like this way?

gulp.task('styles', wrapPipe(function(success, error) {
  return gulp.src('less/*.less')
    .pipe(less().on('error', error))
    .pipe(autoprefixer().on('error', error))
    .pipe(minifyCss().on('error', error))
    .pipe(gulp.dest('app/css'));
}));
@just-boris

This comment has been minimized.

Copy link
Owner Author

@just-boris just-boris commented Jun 3, 2015

@NeXTs Yes, you should do so, because pipes do not propagate errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment