Merge pull request #451 from colkito/fix/user-friendly-insight-error-message

Fix/user friendly insight error message
This commit is contained in:
Gustavo Maximiliano Cortez 2014-05-22 16:42:45 +03:00
commit 41aac17688
3 changed files with 62 additions and 25 deletions

View File

@ -71,7 +71,6 @@ Insight.prototype.getTransactions = function(addresses, cb) {
scheme: self.scheme,
method: 'GET',
path: '/api/addr/' + addr,
headers: {
'Access-Control-Request-Headers': ''
}
@ -108,7 +107,7 @@ Insight.prototype.getTransactions = function(addresses, cb) {
};
Insight.prototype.getUnspent = function(addresses, cb) {
if (!addresses || !addresses.length) return cb([]);
if (!addresses || !addresses.length) return cb(null, []);
var all = [];
@ -125,10 +124,15 @@ Insight.prototype.getUnspent = function(addresses, cb) {
};
this._request(options, function(err, res) {
if (err) {
return cb(err);
}
if (res && res.length > 0) {
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',
data: 'rawtx=' + rawtx,
headers: {
'content-type': 'application/x-www-form-urlencoded'
'Access-Control-Request-Headers': ''
}
};
this._request(options, function(err, res) {
@ -172,10 +176,24 @@ Insight.prototype._request = function(options, callback) {
}
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() {
if (request.readyState === 4) {
if (request.status === 200) {
return callback(null, JSON.parse(request.responseText));
if (request.status === 200 || request.status === 304 || request.status === 0) {
try {
var ret = JSON.parse(request.responseText);
return callback(null, ret);
} catch (e) {
return callback({
message: 'Wrong response from insight'
});
}
} else {
return callback({
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') {
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send(options.data);
} else {
request.send(null);
}
request.send(options.data || null);
} else {
var http = require('http');
var req = http.request(options, function(response) {
var ret;
if (response.statusCode == 200) {
if (response.statusCode == 200 || response.status === 304) {
response.on('data', function(chunk) {
try {
ret = JSON.parse(chunk);
} catch (e) {
callback({
message: "Wrong response from insight"
return callback({
message: 'Wrong response from insight'
});
return;
}
});
response.on('end', function() {
callback(undefined, ret);
return;
return callback(null, ret);
});
} else {
callback({
return callback({
message: 'Error ' + response.statusCode
});
return;
}
});
if (options.data) {
req.write(options.data);
}
req.end();
}
}
};
module.exports = require('soop')(Insight);

View File

@ -502,7 +502,11 @@ Wallet.prototype.getBalance = function(cb) {
var balanceByAddr = {};
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++) {
var u = unspent[i];
var amt = u.amount * COIN;
@ -521,14 +525,19 @@ Wallet.prototype.getBalance = function(cb) {
var amt = u.amount * COIN;
safeBalance += amt;
}
safeBalance = safeBalance / COIN;
return cb(balance, balanceByAddr, safeBalance);
return cb(null, balance, balanceByAddr, safeBalance);
});
};
Wallet.prototype.getUnspent = function(cb) {
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 maxRejectCount = self.totalCopayers - self.requiredCopayers;
@ -538,7 +547,8 @@ Wallet.prototype.getUnspent = function(cb) {
if (uu.indexOf(unspentList[i].txid) === -1)
safeUnspendList.push(unspentList[i]);
}
return cb(safeUnspendList, unspentList);
return cb(null, safeUnspendList, unspentList);
});
};
@ -555,7 +565,7 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
opts.spendUnconfirmed = this.spendUnconfirmed;
}
self.getUnspent(function(safeUnspent) {
this.getUnspent(function(err, safeUnspent) {
var ntxid = self.createTxSync(toAddress, amountSatStr, safeUnspent, opts);
if (ntxid) {
self.sendPublicKeyRing();

View File

@ -114,7 +114,19 @@ angular.module('copay.controllerUtils')
$rootScope.balanceByAddr = {};
$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.balanceByAddr = balanceByAddr;
$rootScope.availableBalance = safeBalance;