I am using spawn to spawn a long running process that sends output over time to stdio, and is read and processed by my nodejs script. The tricky part is that I
As with many things in node, child processes can emit an error event. Add a listener for that and you will be able to catch it (no try-catch needed):
var spawn = require('child_process').spawn;
var child = spawn('foo');
child.on('error', function(err) {
console.log('Oh noez, teh errurz: ' + err);
});
Like mscdex said, you have to attach the 'error' event, however, you should add try catch because some errors, like EPERM doesn't fire the error event and throw Error exception.
var spawn = require('child_process').spawn;
try{
var child = spawn('foo');
child.on('error', function(err) {
console.log('Oh noez, teh errurz: ' + err);
});
}catch(err){
console.log("exception: "+err)
}
You can try to catch it with process uncaughtException event. But i believe rest of the task won't be easy though.
process.on('uncaughtException', function (err) {
console.log(err);
});