I enabled communication between parent and child process in order to send JSON as follows:
Child:
try {
var price1 = parseInt(process.argv[2]);
if (!price1) {
throw new Error('Price in calculations.js undefined');
}
var result = {
'timeStamp' : Date(),
'prices' : { 'player1' : price1, 'player2' : 666}
};
process.send(result);
} catch (e) {
// In case of an error, I get here as expected.
process.send(e);
}
Parent:
var spawn = require('child_process').spawn;
var child = spawn('node', ['calculations.js', 333], {stdio: [null,null,'pipe','ipc']});
child.on('message', function(data) {
if (data instanceof Error) {
// In case of an error, this is never reached.
} else {
// do sthing with JSON object.
}
});
The JSON thing works fine. But if I provoke an error, it doesn't work. I want to send the entire error-object (with message and stack-trace) from child to parent. But it doesn't seem to be an instance of error what I am sending.