From 67f8cabc1852db001c71f931276abbd6b20b5ffa Mon Sep 17 00:00:00 2001 From: ebaauw Date: Mon, 14 Nov 2016 22:07:57 +0100 Subject: [PATCH] issue #15 Resend bridge request after 300ms on ECONNRESET --- lib/HueBridge.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/HueBridge.js b/lib/HueBridge.js index a28e1a97..def532a8 100644 --- a/lib/HueBridge.js +++ b/lib/HueBridge.js @@ -288,12 +288,19 @@ HueBridge.prototype.request = function(method, resource, body) { this.log.debug("%s: hue bridge request: %s", this.name, requestString); request(requestObj, function(err, response, responseBody) { if (err) { - this.log.error("%s: hue bridge communication error %s", this.name, err); - return reject(err); + if (err.code === "ECONNRESET") { + this.log.debug("%s: hue bridge communication error %s - retrying in 300ms", this.name, err.code); + setTimeout(function () { + resolve(this.request(method, resource, body)); + }.bind(this), 300); + return; + } + this.log.error("%s: hue bridge communication error %s", this.name, err.code); + return reject(err.code); } if (response.statusCode != 200) { this.log.error("%s: hue bridge status %s", this.name, response.statusCode); - return reject(err); + return reject(response.statusCode); } // this.log.debug("%s: hue bridge response: %s", this.name, responseBody); var obj; @@ -305,11 +312,11 @@ HueBridge.prototype.request = function(method, resource, body) { } if (util.isArray(obj)) { for (let id in obj) { - let e = obj[id].error; - if (e) { - this.log.error("%s: hue bridge error %d: %s", this.name, e.type, e.description); - return reject(e.type); - } + let e = obj[id].error; + if (e) { + this.log.error("%s: hue bridge error %d: %s", this.name, e.type, e.description); + return reject(e.type); + } } } return resolve(obj);