从生成的 child-process 通过 IPC-channel 发送错误 Object
Sending an Error Object from a spawned child-process over an IPC-channel
我启用了 parent 和 child 进程之间的通信以便发送 JSON 如下:
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.
}
});
JSON 东西工作正常。但如果我引发错误,它就不起作用。我想将整个 error-object(带有消息和 stack-trace)从 child 发送到 parent。但它似乎不是我发送的错误实例。
这是我尝试过的东西,
var Error=function(mes){
this.message=mes;
};
try {
var price1 = parseInt(process.argv[4]);
if (!price1) {
throw new Error('Price in calculations.js undefined');
}
var result = {
'timeStamp' : Date(),
'prices' : { 'player1' : price1, 'player2' : 666}
}; console.log("inside try");
process.send(result);
} catch (e) {
// In case of an error, I get here as expected.
console.log("inside catch");
process.send(e);
}
先创建object Error
再抛出,否则只会传递一个空的object而不是instanceof Error
.
还有 parent
var child = require('child_process').fork(__dirname + '/SO2.js', [333], {stdio: [null,null,'pipe','ipc']});
child.on('message', function(data) {
if(data.timeStamp){
console.log("result received ");
}
else{
// do sthing with JSON object.
console.log("user defined error messege"+data.message + JSON.stringify(data));
}
});
进程不共享内存,因此唯一的通信方式是使用字符串,对象在发送时 JSON 序列化并在接收时 JSON 解析回来。默认情况下,错误对象不能很好地序列化:
JSON.stringify(new Error())
"{}"
此外,JSON 已解析的对象未类型化,因此 instanceof 无法工作。
您可以为错误对象创建序列化挂钩:
Error.prototype.toJSON = function() {
var ret = {
name: this.name,
message: this.message,
stack: this.stack,
__error__: true
};
// Add any custom properties such as .code in file-system errors
Object.keys(this).forEach(function(key) {
if (!ret[key]) {
ret[key] = this[key];
}
}, this);
return ret;
};
在方法定义错误对象序列化之后更好:
JSON.stringify(new Error())
"{"name":"Error","message":"","stack":"Error\n at <anonymous>:2:16\n at Object.InjectedScript._evaluateOn (<anonymous>:762:137)\n at Object.InjectedScript._evaluateAndWrap (<anonymous>:695:34)\n at Object.InjectedScript.evaluate (<anonymous>:609:21)","__error__":true}"
然后自动重建:
function getMessageReceiver(fn) {
return function(data) {
var result = data;
if (data && data.__error__) {
result = new Error();
result.message = data.message;
result.stack = data.stack;
result.name = data.name;
Object.keys(data).forEach(function(key) {
if (!result[key]) {
result[key] = data[key];
}
});
}
return fn.call(this, result);
}
}
最后:
child.on('message', getMessageReceiver(function(data) {
if (data instanceof Error) {
console.log(data.stack); // Stack is from child process
} else {
// do sthing with JSON object.
}
}));
我启用了 parent 和 child 进程之间的通信以便发送 JSON 如下:
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.
}
});
JSON 东西工作正常。但如果我引发错误,它就不起作用。我想将整个 error-object(带有消息和 stack-trace)从 child 发送到 parent。但它似乎不是我发送的错误实例。
这是我尝试过的东西,
var Error=function(mes){
this.message=mes;
};
try {
var price1 = parseInt(process.argv[4]);
if (!price1) {
throw new Error('Price in calculations.js undefined');
}
var result = {
'timeStamp' : Date(),
'prices' : { 'player1' : price1, 'player2' : 666}
}; console.log("inside try");
process.send(result);
} catch (e) {
// In case of an error, I get here as expected.
console.log("inside catch");
process.send(e);
}
先创建object Error
再抛出,否则只会传递一个空的object而不是instanceof Error
.
还有 parent
var child = require('child_process').fork(__dirname + '/SO2.js', [333], {stdio: [null,null,'pipe','ipc']});
child.on('message', function(data) {
if(data.timeStamp){
console.log("result received ");
}
else{
// do sthing with JSON object.
console.log("user defined error messege"+data.message + JSON.stringify(data));
}
});
进程不共享内存,因此唯一的通信方式是使用字符串,对象在发送时 JSON 序列化并在接收时 JSON 解析回来。默认情况下,错误对象不能很好地序列化:
JSON.stringify(new Error())
"{}"
此外,JSON 已解析的对象未类型化,因此 instanceof 无法工作。
您可以为错误对象创建序列化挂钩:
Error.prototype.toJSON = function() {
var ret = {
name: this.name,
message: this.message,
stack: this.stack,
__error__: true
};
// Add any custom properties such as .code in file-system errors
Object.keys(this).forEach(function(key) {
if (!ret[key]) {
ret[key] = this[key];
}
}, this);
return ret;
};
在方法定义错误对象序列化之后更好:
JSON.stringify(new Error())
"{"name":"Error","message":"","stack":"Error\n at <anonymous>:2:16\n at Object.InjectedScript._evaluateOn (<anonymous>:762:137)\n at Object.InjectedScript._evaluateAndWrap (<anonymous>:695:34)\n at Object.InjectedScript.evaluate (<anonymous>:609:21)","__error__":true}"
然后自动重建:
function getMessageReceiver(fn) {
return function(data) {
var result = data;
if (data && data.__error__) {
result = new Error();
result.message = data.message;
result.stack = data.stack;
result.name = data.name;
Object.keys(data).forEach(function(key) {
if (!result[key]) {
result[key] = data[key];
}
});
}
return fn.call(this, result);
}
}
最后:
child.on('message', getMessageReceiver(function(data) {
if (data instanceof Error) {
console.log(data.stack); // Stack is from child process
} else {
// do sthing with JSON object.
}
}));