[英]How to catch error from a forked child process?
Node.js v8.11.1 Node.js v8.11.1
There are two files: parent.js and child.js.有两个文件:parent.js 和 child.js。
Parent家长
const { fork } = require('child_process');
const forked = fork('./child');
forked.on('message', (msg) => {
console.log('Message from child', msg);
forked.kill();
});
forked.on('error', (err) => {
console.log('Error from child', err);
});
forked.send({ hello: 'world' });
Child孩子
process.on('message', (msg) => {
console.log('Message from parent:', msg);
});
try {
let counter = 0;
seTimeout(() => {
process.send({ counter: counter++ });
}, 1000);
} catch (err) {
throw new Error('not good: ' + err);
}
There is an intentional typo in the child script - seTimeout
.子脚本中有一个故意的错字 -
seTimeout
。 When I execute parent node parent.js
, I get:当我执行父
node parent.js
,我得到:
/media/trex/safe/Development/child.js:12
throw new Error('not good: ' + err);
^
Error: not good: ReferenceError: seTimeout is not defined
at Object.<anonymous> (/media/trex/safe/Development/siren/sentinl-private/server/lib/actions/report/child.js:12:9)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
The error was not caught by the parent.错误未被父级捕获。 How to catch the error by the parent?
如何捕捉父母的错误?
As I see here https://github.com/nodejs/node/issues/15734 this is working as intended.正如我在这里看到的https://github.com/nodejs/node/issues/15734这是按预期工作的。 Maybe there is another event to catch errors or something else?
也许还有另一个事件可以捕获错误或其他什么?
Now I can catch an error the following way.现在我可以通过以下方式捕获错误。 But I'm not sure it is the best way to do it.
但我不确定这是最好的方法。
Parent家长
const { fork } = require('child_process');
const forked = fork('./child');
forked.on('message', (msg) => {
if (msg.error) {
console.error(msg.error);
} else {
console.log('Message from child', msg);
}
forked.kill();
});
forked.send({ hello: 'world' });
Child孩子
process.on('message', (msg) => {
console.log('Message from parent:', msg);
});
try {
let counter = 0;
seTimeout(() => {
process.send({ counter: counter++ });
}, 1000);
} catch (err) {
process.send({error: err.message});
}
Why not to use the array of the option.stdio
?为什么不使用
option.stdio
的数组? This array has three elements [subprocess.stdin, subprocess.stdout, subprocess.stderr]
, thus you just want to console the errors and not anything else.这个数组有三个元素
[subprocess.stdin, subprocess.stdout, subprocess.stderr]
,因此你只想控制错误而不是其他任何东西。
Therefore you'd use stdio: ['ignore', 'ignore', 'inherit']
因此你会使用
stdio: ['ignore', 'ignore', 'inherit']
const { execSync } = require('child_process')
try {
execSync('node myNodeChildScript.js', { stdio: ['ignore', 'ignore', 'inherit'] })
} catch (err) {
console.error(Error(err))
process.exit(1)
}
console.log('Some log') // this will not be output to parent
if (someError()) {
console.error('There was an error') // this will be output to parent console
process.exit(1) // exit with error
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.