Skip to content

Instantly share code, notes, and snippets.

@NickHeiner
Created February 19, 2015 22:00
Show Gist options
  • Save NickHeiner/e83046b218184ac9a656 to your computer and use it in GitHub Desktop.
Save NickHeiner/e83046b218184ac9a656 to your computer and use it in GitHub Desktop.
Handling errors
// index.js
function myTask(args) {
if (somethingBadHappened()) {
throw new Error('Too many cooks will spoil the broth.');
}
}
// tasks/task.js
module.exports = function(grunt) {
// Sync version
grunt.registerMultiTask('my-task', function() {
var myTask = require('../');
var options = this.options();
try {
myTask(options);
} catch (e) {
// Use the grunt.fail API as you see fit.
grunt.fail.fatal(e);
}
});
// Async version
grunt.registerMultiTask('my-task', function() {
var myTask = require('../');
var options = this.options();
var done = this.async();
myTask(options).then(done).fail(function(err) {
// Do whatever grunt-specific error handling you want.
done(err);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment