Jonny Flowers
Jonny Flowers

Reputation: 631

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

Upvotes: 1

Views: 69

Answers (1)

Yuri Zarubin
Yuri Zarubin

Reputation: 11677

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

Upvotes: 1

Related Questions