From be62d895b1e30fda79a2eeb5c3faaab57ad4fcfe Mon Sep 17 00:00:00 2001 From: Marian Oancea Date: Wed, 28 Jan 2015 23:57:45 +0200 Subject: [PATCH] added error handling for node connection --- models/node.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/models/node.js b/models/node.js index 6be3a05..1d5ff65 100644 --- a/models/node.js +++ b/models/node.js @@ -15,7 +15,8 @@ var Node = function Node(options, id) mining: false, block: { height: 0, - hash: '?' + hash: '?', + timestamp: 0 } } @@ -33,10 +34,23 @@ Node.prototype.update = function() var eth = this.web3.eth; - this.info.stats.peers = eth.peerCount; - this.info.stats.mining = eth.mining; - this.info.stats.block.height = eth.number; - this.info.stats.block.hash = eth.block(this.info.stats.block.height).hash; + try { + this.info.stats.peers = parseInt(eth.peerCount); + } + catch (err) { + this.info.stats.peers = 0; + } + + if(this.info.stats.peers > 0) { + this.info.stats.block.height = eth.number; + var block = eth.block(this.info.stats.block.height) + this.info.stats.block.hash = block.hash; + this.info.stats.block.timestamp = block.timestamp; + this.info.stats.mining = eth.mining; + this.info.stats.active = true; + } else { + this.info.stats.active = false; + } return this.info; };