1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Better error handling in PushBullet nodes

This commit is contained in:
Nick O'Leary 2015-04-20 10:50:36 +01:00
parent 889b62cf70
commit 66581d71e1
2 changed files with 47 additions and 24 deletions

View File

@ -38,15 +38,30 @@ module.exports = function(RED) {
function PushbulletConfig(n) { function PushbulletConfig(n) {
RED.nodes.createNode(this, n); RED.nodes.createNode(this, n);
this.n = n;
this.name = n.name; this.name = n.name;
this._inputNodes = []; this._inputNodes = [];
this.emitter = new EventEmitter(); 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; var self = this;
// sort migration from old node // sort migration from old node
var apikey; var apikey;
if(n._migrate) { if(this.n._migrate) {
apikey = n._apikey; apikey = this.n._apikey;
this.credentials = {apikey:apikey}; this.credentials = {apikey:apikey};
} }
else if(this.credentials) { else if(this.credentials) {
@ -61,24 +76,26 @@ module.exports = function(RED) {
pusher.me(function(err, me) { pusher.me(function(err, me) {
if(err) { if(err) {
reject(err); reject(err);
return onError(err, self); } else {
resolve(me);
} }
resolve(me);
}); });
}).otherwise(function(err) {
onError(err, self);
}); });
// get latest timestamp // get latest timestamp
this.last = when.promise(function(resolve) { this.last = when.promise(function(resolve) {
pusher.history({limit:1}, function(err, res) { pusher.history({limit:1}, function(err, res) {
if(err) { if(err) {
resolve(0); resolve(0);
return onError(err, self); } else {
} try {
try { resolve(res.pushes[0].modified);
resolve(res.pushes[0].modified); }
} catch(ex){
catch(ex){ self.warn('Unable to get history.');
self.warn('Unable to get history.'); resolve(0);
resolve(0); }
} }
}); });
}); });
@ -93,19 +110,10 @@ module.exports = function(RED) {
} }
this.on("close", function() { this.on("close", function() {
if(self.stream) {
self.stream.close();
}
self._inputNodes.length = 0; self._inputNodes.length = 0;
}); });
} }
RED.nodes.registerType("pushbullet-config", PushbulletConfig, {
credentials: {
apikey: {type: "password"}
}
});
PushbulletConfig.prototype.onConfig = function(type, cb) { PushbulletConfig.prototype.onConfig = function(type, cb) {
this.emitter.on(type, cb); this.emitter.on(type, cb);
} }
@ -133,6 +141,15 @@ module.exports = function(RED) {
}); });
stream.connect(); stream.connect();
this.stream = stream; 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) { if(configNode) {
configNode.initialise();
this.pusher = configNode.pusher; this.pusher = configNode.pusher;
configNode.onConfig('error', function(err) { configNode.onConfig('error', function(err) {
self.error(err); self.error(err);
@ -341,8 +359,12 @@ module.exports = function(RED) {
else if(deviceid === "") { else if(deviceid === "") {
try { try {
when(configNode.me).then(function(me) { when(configNode.me).then(function(me) {
deviceid = me.email; if (me) {
self.pushMsg(pushtype, deviceid, title, msg); deviceid = me.email;
self.pushMsg(pushtype, deviceid, title, msg);
} else {
self.error("Unable to push",msg);
}
}); });
return; return;
} }
@ -502,6 +524,7 @@ module.exports = function(RED) {
var self = this; var self = this;
var config = RED.nodes.getNode(n.config); var config = RED.nodes.getNode(n.config);
if(config) { if(config) {
config.initialise();
config.registerInputNode(this); config.registerInputNode(this);
config.onConfig('error', function(err) { config.onConfig('error', function(err) {
self.error(err); self.error(err);

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-pushbullet", "name" : "node-red-node-pushbullet",
"version" : "0.0.3", "version" : "0.0.4",
"description" : "A Node-RED node to send alerts via Pushbullet", "description" : "A Node-RED node to send alerts via Pushbullet",
"dependencies" : { "dependencies" : {
"pushbullet": "1.4.*", "pushbullet": "1.4.*",