简体   繁体   中英

Error Handling on yEnc in nodejs

I am using the yEnc module with 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.

What is the correct way to do this? Thanks

Try/catch works fine in this case, as yEnc.decode() is a synchronous operation. The only time try/catch is not suitable is when attempting to catch errors from asynchronous operations.

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)  
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM