added a user friendly message when insight doesnt work

This commit is contained in:
Mario Colque 2014-05-21 18:03:11 -03:00
parent 194ddc8079
commit 6c973d7f88
3 changed files with 62 additions and 25 deletions

View File

@ -71,7 +71,6 @@ Insight.prototype.getTransactions = function(addresses, cb) {
scheme: self.scheme, scheme: self.scheme,
method: 'GET', method: 'GET',
path: '/api/addr/' + addr, path: '/api/addr/' + addr,
headers: { headers: {
'Access-Control-Request-Headers': '' 'Access-Control-Request-Headers': ''
} }
@ -108,7 +107,7 @@ Insight.prototype.getTransactions = function(addresses, cb) {
}; };
Insight.prototype.getUnspent = function(addresses, cb) { Insight.prototype.getUnspent = function(addresses, cb) {
if (!addresses || !addresses.length) return cb([]); if (!addresses || !addresses.length) return cb(null, []);
var all = []; var all = [];
@ -125,10 +124,15 @@ Insight.prototype.getUnspent = function(addresses, cb) {
}; };
this._request(options, function(err, res) { this._request(options, function(err, res) {
if (err) {
return cb(err);
}
if (res && res.length > 0) { if (res && res.length > 0) {
all = all.concat(res); all = all.concat(res);
} }
return cb(all);
return cb(null, all);
}); });
}; };
@ -142,7 +146,7 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) {
path: '/api/tx/send', path: '/api/tx/send',
data: 'rawtx=' + rawtx, data: 'rawtx=' + rawtx,
headers: { headers: {
'content-type': 'application/x-www-form-urlencoded' 'Access-Control-Request-Headers': ''
} }
}; };
this._request(options, function(err, res) { this._request(options, function(err, res) {
@ -172,10 +176,24 @@ Insight.prototype._request = function(options, callback) {
} }
request.open(options.method, url, true); request.open(options.method, url, true);
request.timeout = 10000;
request.ontimeout = function() {
return callback({
message: 'Insight request timeout. Please check your Insight settings or the Insight server status.'
});
};
request.onreadystatechange = function() { request.onreadystatechange = function() {
if (request.readyState === 4) { if (request.readyState === 4) {
if (request.status === 200) { if (request.status === 200 || request.status === 304 || request.status === 0) {
return callback(null, JSON.parse(request.responseText)); try {
var ret = JSON.parse(request.responseText);
return callback(null, ret);
} catch (e) {
return callback({
message: 'Wrong response from insight'
});
}
} else { } else {
return callback({ return callback({
message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText
@ -186,42 +204,39 @@ Insight.prototype._request = function(options, callback) {
if (options.method === 'POST') { if (options.method === 'POST') {
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send(options.data);
} else {
request.send(null);
} }
request.send(options.data || null);
} else { } else {
var http = require('http'); var http = require('http');
var req = http.request(options, function(response) { var req = http.request(options, function(response) {
var ret; var ret;
if (response.statusCode == 200) { if (response.statusCode == 200 || response.status === 304) {
response.on('data', function(chunk) { response.on('data', function(chunk) {
try { try {
ret = JSON.parse(chunk); ret = JSON.parse(chunk);
} catch (e) { } catch (e) {
callback({ return callback({
message: "Wrong response from insight" message: 'Wrong response from insight'
}); });
return;
} }
}); });
response.on('end', function() { response.on('end', function() {
callback(undefined, ret); return callback(null, ret);
return;
}); });
} else { } else {
callback({ return callback({
message: 'Error ' + response.statusCode message: 'Error ' + response.statusCode
}); });
return;
} }
}); });
if (options.data) { if (options.data) {
req.write(options.data); req.write(options.data);
} }
req.end(); req.end();
} }
} };
module.exports = require('soop')(Insight); module.exports = require('soop')(Insight);

View File

@ -501,7 +501,11 @@ Wallet.prototype.getBalance = function(cb) {
var balanceByAddr = {}; var balanceByAddr = {};
var COIN = bitcore.util.COIN; var COIN = bitcore.util.COIN;
this.getUnspent(function(safeUnspent, unspent) { this.getUnspent(function(err, safeUnspent, unspent) {
if (err) {
return cb(err);
}
for (var i = 0; i < unspent.length; i++) { for (var i = 0; i < unspent.length; i++) {
var u = unspent[i]; var u = unspent[i];
var amt = u.amount * COIN; var amt = u.amount * COIN;
@ -520,14 +524,19 @@ Wallet.prototype.getBalance = function(cb) {
var amt = u.amount * COIN; var amt = u.amount * COIN;
safeBalance += amt; safeBalance += amt;
} }
safeBalance = safeBalance / COIN; safeBalance = safeBalance / COIN;
return cb(balance, balanceByAddr, safeBalance); return cb(null, balance, balanceByAddr, safeBalance);
}); });
}; };
Wallet.prototype.getUnspent = function(cb) { Wallet.prototype.getUnspent = function(cb) {
var self = this; var self = this;
this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) { this.blockchain.getUnspent(this.getAddressesStr(), function(err, unspentList) {
if (err) {
return cb(err);
}
var safeUnspendList = []; var safeUnspendList = [];
var maxRejectCount = self.totalCopayers - self.requiredCopayers; var maxRejectCount = self.totalCopayers - self.requiredCopayers;
@ -537,7 +546,8 @@ Wallet.prototype.getUnspent = function(cb) {
if (uu.indexOf(unspentList[i].txid) === -1) if (uu.indexOf(unspentList[i].txid) === -1)
safeUnspendList.push(unspentList[i]); safeUnspendList.push(unspentList[i]);
} }
return cb(safeUnspendList, unspentList);
return cb(null, safeUnspendList, unspentList);
}); });
}; };
@ -554,7 +564,7 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
opts.spendUnconfirmed = this.spendUnconfirmed; opts.spendUnconfirmed = this.spendUnconfirmed;
} }
self.getUnspent(function(safeUnspent) { this.getUnspent(function(err, safeUnspent) {
var ntxid = self.createTxSync(toAddress, amountSatStr, safeUnspent, opts); var ntxid = self.createTxSync(toAddress, amountSatStr, safeUnspent, opts);
if (ntxid) { if (ntxid) {
self.sendPublicKeyRing(); self.sendPublicKeyRing();

View File

@ -114,7 +114,19 @@ angular.module('copay.controllerUtils')
$rootScope.balanceByAddr = {}; $rootScope.balanceByAddr = {};
$rootScope.updatingBalance = true; $rootScope.updatingBalance = true;
w.getBalance(function(balance, balanceByAddr, safeBalance) { w.getBalance(function(err, balance, balanceByAddr, safeBalance) {
if (err) {
$rootScope.$flashMessage = {
type: 'error',
message: 'Error: ' + err.message
};
$rootScope.$digest();
console.error('Error: ' + err.message); //TODO
return null;
}
$rootScope.totalBalance = balance; $rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr; $rootScope.balanceByAddr = balanceByAddr;
$rootScope.availableBalance = safeBalance; $rootScope.availableBalance = safeBalance;