// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleRescue

Error Handling in gulp

Should a gulp build fail on fatal errors, task errors, or warnings? It depends on context: CI, watch mode, or local builds. How to handle each.

Art+Logic May 19, 20144 min read

From the Art+Logic archive · written in 2014. The tools may have moved on; the thinking still holds.

Photo of inside fo tackle box by Harrison Kugler on Unsplash

Consider three reference points on the spectrum of errors that might occur in a build system:

  1. The build system broke (we’ll call this “fatal”): the makefile has a bug, a file resource disappeared, a gremlin burrowed through the ram
  2. A critical build task failed (“error”): the coffeescript has a syntax error, the server couldn’t start
  3. A validation task failed (“warning): the sass has a lint warning, a behavior test failed

Should the build fail for each of these error types?  That depends on the context:

How can these error permutations be handled in a gulp build?

Error handling in gulpfile.js

For typical gulp plugin usage, any errors that occur will be emitted on the stream and can be easily caught and logged:

gulp.task('coffee', function() {
   gulp.src(testfiles)
      .pipe(coffee())
      .on('error', gutil.log);
}

That’s only the beginning of the story.  To handle the different build contexts described above we’ll write an error handler (to replace gutil.log in the above code) that will log errors and halt the build process when appropriate.

Determine the build context

Since the same tasks can be run in different contexts (CI server, local dev, watch) we’ll use a flag that sets the fatality error level:

var fatalLevel = require('yargs').argv.fatal;

This flag can be set through a gulp command line option:

$ gulp                  # defaults to fatal=error as we'll see in a moment
$ gulp --fatal=error
$ gulp --fatal=warning
$ gulp --fatal=off      # no errors should kill the build

Determine if the error is fatal

Assuming each error is tagged with a level of error or warning, then we can write a function to determine whether an error is fatal or not based on the command line fatality flag:

var ERROR_LEVELS = ['error', 'warning'];
function isFatal(level) {
   return ERROR_LEVELS.indexOf(level) <= ERROR_LEVELS.indexOf(fatalLevel || 'error');
}

isFatal() returns true if the given level is equal to or more severe than the configured fatality error level. If the fatality level is off this always returns true. It defaults the fatality level to error if it has not yet been set by the user or some task default.

Error handler

function handleError(level, error) {
   gutil.log(error.message);
   if (isFatal(level)) {
      process.exit(1);
   }
}
function onError(error) { handleError.call(this, 'error', error);}
function onWarning(error) { handleError.call(this, 'warning', error);}

handleError() simply logs the error message and kills the process (with an error exit status) if the error level matches or exceeds the fatality level. onError and onWarning are helpers that can be passed directly as callbacks to the on('error', callback) stream method.

Insert the error handler into task streams

Handling errors and warnings in task streams is now easy.  Our CoffeeScript lint task will handle lint errors as warnings, and our CoffeeScript compile task will handle compilation errors as errors.

gulp.task('coffeelint', function() {
   gulp.src(testfiles)
      .pipe(coffeelint())
      .on('error', onWarning);
}
gulp.task('coffee', function() {
   gulp.src(testfiles)
      .pipe(coffee())
      .on('error', onError);
}

We can default the fatality level to off for the watch task so that it never fails, but still logs warnings and errors:

gulp.task('watch', function() {
   fatalLevel = fatalLevel || 'off';
   gulp.watch(testfiles, ['coffeelint', 'coffee']);
});

Example gulpfile

Here’s an example gulpfile in its entirety:

var gulp = require('gulp');
var gutil = require('gulp-util');
var jshint = require('gulp-jshint');
// Command line option:
//  --fatal=[warning|error|off]
var fatalLevel = require('yargs').argv.fatal;
var ERROR_LEVELS = ['error', 'warning'];
// Return true if the given level is equal to or more severe than
// the configured fatality error level.
// If the fatalLevel is 'off', then this will always return false.
// Defaults the fatalLevel to 'error'.
function isFatal(level) {
   return ERROR_LEVELS.indexOf(level) <= ERROR_LEVELS.indexOf(fatalLevel || 'error');
}
// Handle an error based on its severity level.
// Log all levels, and exit the process for fatal levels.
function handleError(level, error) {
   gutil.log(error.message);
   if (isFatal(level)) {
      process.exit(1);
   }
}
// Convenience handler for error-level errors.
function onError(error) { handleError.call(this, 'error', error);}
// Convenience handler for warning-level errors.
function onWarning(error) { handleError.call(this, 'warning', error);}
var testfiles = ['error.js', 'warning.js'];
// Task that emits an error that's treated as a warning.
gulp.task('warning', function() {
   gulp.src(testfiles).
      pipe(jshint()).
      pipe(jshint.reporter('fail')).
      on('error', onWarning);
});
// Task that emits an error that's treated as an error.
gulp.task('error', function() {
   gulp.src(testfiles).
      pipe(jshint()).
      pipe(jshint.reporter('fail')).
      on('error', onError);
});
gulp.task('watch', function() {
   // By default, errors during watch should not be fatal.
   fatalLevel = fatalLevel || 'off';
   gulp.watch(testfiles, ['error']);
});
gulp.task('default', ['watch']);

What else?

There’s more potential complexity to error handling in gulp than I’ve covered.

For example, gulp-plumber helps to prevent stream errors from breaking streams (though it may be unnecessary since gulp’s watch method was recently patched to better handle errors).

There have been lengthy discussions about how plugins should report errors, such as whether gulp-jshint should fail a build.  The questions of when and how to return an error seem unsettled in the plugin author community, which makes sense since expectations might be quite different per user, project, and context.

There’s also a plan (discussed in controlling failing builds) to support deferring a build failure until all tasks have run so that a jshint error causes a failure but still allows other tasks to complete.

It looks like the next few months, and gulp 4 (anyone know the status?), will bring progressive change to error handling and hopefully further solidify best practices across the ecosystem.

keep going

More to explore in Rescue.

All Rescue resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…