Is there a way to catch when a GruntJS task fails and act upon it?
The --force
flag doesn't help, because I need to know if something broke along the way, and do something about it.
I tried some arrangement similar to a try-catch, however it doesn't work. That is because grunt.registerTask
pushes tasks into a queue - the execution is not synchronous.
grunt.registerTask('foo', "My foo task.", function() {
try {
grunt.task.run('bar');
} catch (ex) {
// Handle the failure without breaking the task queue
}
});
Creative javascript ideas are welcome as well as GruntJS know-how.
This ugly beast should work for you:
grunt.registerMultiTask('foo-task', 'my foo task', function() {
try {
console.log(this.data);
throw new Error('something bad happened!');
} catch (e) {
console.log('Ooops:', e);
}
return true;
});
grunt.registerTask('foo', 'Runs foo.', function() {
grunt.config('foo-task', {
hello: 'world'
});
grunt.task.run('foo-task');
});
Run it via: grunt foo
Output:
Running "foo" task
Running "foo-task:hello" (foo-task) task
world
Ooops: Error: something bad happened!
at Object.<anonymous>
<stacktrace here>
Done, without errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With