[英]Error Handling on yEnc in nodejs
I am using the yEnc module with Node. 我将yEnc模块与Node一起使用。
I have been trying to work out how to carry out error handling as the module has no token for an err. 我一直在尝试找出如何执行错误处理的方法,因为该模块没有错误标记。 How would I perform error handling?
我将如何执行错误处理?
The code I have is as follows: 我的代码如下:
exports.emailSNSclient = SNSClient(function(err, message) {
var payload = JSON.parse(yEnc.decode(message.Message));
logger.info(payload);
I am unsure if try catch would work because I have been led to believe it doesn't work too well with node. 我不确定try catch是否会起作用,因为我被认为无法在node上很好地工作。
What is the correct way to do this? 正确的方法是什么? Thanks
谢谢
Try/catch works fine in this case, as yEnc.decode() is a synchronous operation. 在这种情况下,try / catch可以正常工作,因为yEnc.decode()是同步操作。 The only time try/catch is not suitable is when attempting to catch errors from asynchronous operations.
仅当尝试从异步操作捕获错误时,try / catch才是不合适的。
The following will work fine: 以下将正常工作:
exports.emailSNSclient = SNSClient(function(err, message) {
try {
var payload = JSON.parse(yEnc.decode(message.Message));
logger.info(payload);
} catch(e) {
console.log(e)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.