在程序错误时完成外部模块中的写入流
Finishing write stream in external module upon program error
如何使外部模块中的写入流在发生错误时完成写入?
我已尝试使用以下代码,但在流完成之前仍然抛出错误。我还尝试将回调(包含 throw err;
)传递给 stop()
函数,并使用 logfile.on('end', () => { callback(); })
使其执行,但这没有做任何事情。
index.js
process.on('uncaughtException', (err) => {
logger.stop(); // function in external module
throw err;
});
...
Oh no! Waffles broke the code, because they're evil!
logger.js
module.exports = {
...
stop: () => {
logfile.end(); // logfile is a global variable containing a write stream
}
}
问题可以通过使用console.log(err);
显示错误来解决,防止在外部模块中显示错误调用process.exit(1);
后程序自动关闭,当finish
事件发生时被调用。
index.js
process.on('uncaughtException', (err) => {
console.error(err);
logger.stop();
});
...
Oh no! Waffles broke the code, because they're evil!
logger.js
module.exports = {
...
stop: () => {
logfile.on('finish', () => { process.exit(1); });
logfile.end();
}
}
如何使外部模块中的写入流在发生错误时完成写入?
我已尝试使用以下代码,但在流完成之前仍然抛出错误。我还尝试将回调(包含 throw err;
)传递给 stop()
函数,并使用 logfile.on('end', () => { callback(); })
使其执行,但这没有做任何事情。
index.js
process.on('uncaughtException', (err) => {
logger.stop(); // function in external module
throw err;
});
...
Oh no! Waffles broke the code, because they're evil!
logger.js
module.exports = {
...
stop: () => {
logfile.end(); // logfile is a global variable containing a write stream
}
}
问题可以通过使用console.log(err);
显示错误来解决,防止在外部模块中显示错误调用process.exit(1);
后程序自动关闭,当finish
事件发生时被调用。
index.js
process.on('uncaughtException', (err) => {
console.error(err);
logger.stop();
});
...
Oh no! Waffles broke the code, because they're evil!
logger.js
module.exports = {
...
stop: () => {
logfile.on('finish', () => { process.exit(1); });
logfile.end();
}
}