Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8872

Node.js: 子プロセスの例外を親プロセスに送る方法

$
0
0

Node.jsのchild_processモジュールのforkで起動した子プロセスにて、例外が発生したとき、その例外を親プロセスに送る方法です。

const{fork}=require('child_process')if(process.send){// 子プロセスの処理process.on('uncaughtException',error=>{process.send(error)// 例外はメッセージとして送るprocess.exit(1)})thrownewError('Something wrong')}else{// 親プロセスの処理constchildProcess=fork(__filename,[],{serialization:'advanced'// このオプションが必須})// 例外はメッセージとして受け取るchildProcess.on('message',message=>{if(messageinstanceofError){console.log('Child process error:')console.error(message)}})}

Viewing all articles
Browse latest Browse all 8872

Trending Articles