Try-catch tasks in GruntJS

Is there a way to catch when a GruntJS task crashes and acts on it?

The --force flag --force not help, because I need to know something is broken on the way and something can be done about it.

I tried some kind of circuit that looks like an attempt, but it doesn't work. This is because grunt.registerTask tasks - 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 ideas from javascript and GruntJS know-how are welcome.

+6
source share
2 answers

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 through: 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. 
+1
source

I have not tested it, but, as in any Javascript code, you could add an event listener to grunt the object and fire the event from tasks when you need it ...

I don’t know if this can exactly match your question, but I would try.

Hope this helps!

0
source

Source: https://habr.com/ru/post/981172/


All Articles