From ded38f8083401dc2f5607fcb6340292febabef3d Mon Sep 17 00:00:00 2001 From: Mike Blackstock Date: Thu, 19 Nov 2020 17:09:58 -0800 Subject: [PATCH] added try/catch --- influxdb.js | 96 ++++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/influxdb.js b/influxdb.js index 19b9431..5da2f70 100755 --- a/influxdb.js +++ b/influxdb.js @@ -121,59 +121,65 @@ module.exports = function (RED) { node.error(RED._("influxdb.errors.nomeasurement"), msg); return; } - var point; - if (_.isArray(msg.payload) && msg.payload.length > 0) { - // array of arrays - if (_.isArray(msg.payload[0]) && msg.payload[0].length > 0) { - msg.payload.forEach(element => { - point = new Point(measurement); - - var fields = element[0]; + try { + if (_.isArray(msg.payload) && msg.payload.length > 0) { + // array of arrays + if (_.isArray(msg.payload[0]) && msg.payload[0].length > 0) { + msg.payload.forEach(element => { + let point = new Point(measurement); + + let fields = element[0]; + addFieldsToPoint(point, fields); + + let tags = element[1]; + for (const prop in tags) { + point.tag(prop, tags[prop]); + } + node.client.writePoint(point); + }); + } else { + // array of non-arrays, assume one point with both fields and tags + let point = new Point(measurement); + + let fields = msg.payload[0]; addFieldsToPoint(point, fields); - - var tags = element[1]; + + const tags = msg.payload[1]; for (const prop in tags) { point.tag(prop, tags[prop]); } - node.client.writePoint(point); - }); - } else { - // array of non-arrays, assume one point with both fields and tags - point = new Point(measurement); - - var fields = msg.payload[0]; - addFieldsToPoint(point, fields); - - const tags = msg.payload[1]; - for (const prop in tags) { - point.tag(prop, tags[prop]); + + node.client.writePoint(point) } - - node.client.writePoint(point) - } - } else { - if (_.isPlainObject(msg.payload)) { - point = new Point(measurement); - var fields = msg.payload; - addFieldsToPoint(point, fields); - node.client.writePoint(point); } else { - // just a value - point = new Point(measurement); - var value = msg.payload; - addFieldToPoint(point, 'value', value); - node.client.writePoint(point); + if (_.isPlainObject(msg.payload)) { + let point = new Point(measurement); + let fields = msg.payload; + addFieldsToPoint(point, fields); + node.client.writePoint(point); + } else { + // just a value + let point = new Point(measurement); + let value = msg.payload; + addFieldToPoint(point, 'value', value); + node.client.writePoint(point); + } } + + node.client + .close() + .catch(error => { + msg.influx_error = { + errorMessage: error + }; + node.error(error, msg); + }) + } catch (error) { + msg.influx_error = { + errorMessage: error + }; + node.error(error, msg); } - - node.client - .close() - .catch(error => { - msg.influx_error = { - errorMessage: error - }; - node.error(error, msg); - }) } function queryRows(msg, node) {