Simplification in parseMessage() code
fgalan opened this issue · 1 comments
fgalan commented
In parseMessage() function at https://github.com/telefonicaid/iotagent-json/blob/master/lib/commonBindings.js we have:
try {
stringMessage = message.toString();
try {
parsedMessage = JSON.parse(stringMessage);
} catch (e) {
parsedMessage = message.toString('hex');
}
config.getLogger().debug(context, 'stringMessage: [%s] parsedMessage: [%s]', stringMessage, parsedMessage);
messageArray = [];
if (Array.isArray(parsedMessage)) {
messageArray = parsedMessage;
} else {
messageArray.push(parsedMessage);
}
} catch (e) {
config.getLogger().debug(context, 'Parse error treating message [%s]: %j', stringMessage, e);
messageArray = null;
parsedMessageError = true;
}
I think it can be simplified to:
try {
stringMessage = message.toString();
try {
parsedMessage = JSON.parse(stringMessage);
} catch (e) {
parsedMessage = message.toString('hex');
}
config.getLogger().debug(context, 'stringMessage: [%s] parsedMessage: [%s]', stringMessage, parsedMessage);
messageArray = [];
if (Array.isArray(parsedMessage)) {
messageArray = parsedMessage;
} else {
messageArray.push(parsedMessage);
}
}
given now it's not possible the external catch to be called (the internal call "bypassed" it)