From 66581d71e1214c7f4e1978c175c4659f332de1fe Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 20 Apr 2015 10:50:36 +0100 Subject: [PATCH] Better error handling in PushBullet nodes --- social/pushbullet/57-pushbullet.js | 69 ++++++++++++++++++++---------- social/pushbullet/package.json | 2 +- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/social/pushbullet/57-pushbullet.js b/social/pushbullet/57-pushbullet.js index 030a1146..5be52ef3 100644 --- a/social/pushbullet/57-pushbullet.js +++ b/social/pushbullet/57-pushbullet.js @@ -38,15 +38,30 @@ module.exports = function(RED) { function PushbulletConfig(n) { RED.nodes.createNode(this, n); + this.n = n; this.name = n.name; this._inputNodes = []; this.emitter = new EventEmitter(); + this.initialised = false; + } + + RED.nodes.registerType("pushbullet-config", PushbulletConfig, { + credentials: { + apikey: {type: "password"} + } + }); + + PushbulletConfig.prototype.initialise = function() { + if (this.initialised) { + return; + } + this.initialised = true; var self = this; // sort migration from old node var apikey; - if(n._migrate) { - apikey = n._apikey; + if(this.n._migrate) { + apikey = this.n._apikey; this.credentials = {apikey:apikey}; } else if(this.credentials) { @@ -61,24 +76,26 @@ module.exports = function(RED) { pusher.me(function(err, me) { if(err) { reject(err); - return onError(err, self); + } else { + resolve(me); } - resolve(me); }); + }).otherwise(function(err) { + onError(err, self); }); // get latest timestamp this.last = when.promise(function(resolve) { pusher.history({limit:1}, function(err, res) { if(err) { resolve(0); - return onError(err, self); - } - try { - resolve(res.pushes[0].modified); - } - catch(ex){ - self.warn('Unable to get history.'); - resolve(0); + } else { + try { + resolve(res.pushes[0].modified); + } + catch(ex){ + self.warn('Unable to get history.'); + resolve(0); + } } }); }); @@ -93,19 +110,10 @@ module.exports = function(RED) { } this.on("close", function() { - if(self.stream) { - self.stream.close(); - } self._inputNodes.length = 0; }); } - RED.nodes.registerType("pushbullet-config", PushbulletConfig, { - credentials: { - apikey: {type: "password"} - } - }); - PushbulletConfig.prototype.onConfig = function(type, cb) { this.emitter.on(type, cb); } @@ -133,6 +141,15 @@ module.exports = function(RED) { }); stream.connect(); this.stream = stream; + this.on("close",function() { + try { + this.stream.close(); + } catch(err) { + // Ignore error if not connected + } + }); + + } }; @@ -315,6 +332,7 @@ module.exports = function(RED) { } if(configNode) { + configNode.initialise(); this.pusher = configNode.pusher; configNode.onConfig('error', function(err) { self.error(err); @@ -341,8 +359,12 @@ module.exports = function(RED) { else if(deviceid === "") { try { when(configNode.me).then(function(me) { - deviceid = me.email; - self.pushMsg(pushtype, deviceid, title, msg); + if (me) { + deviceid = me.email; + self.pushMsg(pushtype, deviceid, title, msg); + } else { + self.error("Unable to push",msg); + } }); return; } @@ -502,6 +524,7 @@ module.exports = function(RED) { var self = this; var config = RED.nodes.getNode(n.config); if(config) { + config.initialise(); config.registerInputNode(this); config.onConfig('error', function(err) { self.error(err); diff --git a/social/pushbullet/package.json b/social/pushbullet/package.json index 7362a470..c0fbb6e9 100644 --- a/social/pushbullet/package.json +++ b/social/pushbullet/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-pushbullet", - "version" : "0.0.3", + "version" : "0.0.4", "description" : "A Node-RED node to send alerts via Pushbullet", "dependencies" : { "pushbullet": "1.4.*",