Created
February 19, 2015 22:00
-
-
Save NickHeiner/e83046b218184ac9a656 to your computer and use it in GitHub Desktop.
Handling errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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