better insight error handling

This commit is contained in:
Matias Alejo Garcia 2015-09-08 00:57:59 -03:00
parent cf59b73023
commit fdb3e35864
3 changed files with 18 additions and 8 deletions

View File

@ -32,7 +32,7 @@ function BlockChainExplorer(opts) {
url: url
});
default:
throw new Error('Provider ' + provider + ' not supperted.');
throw new Error('Provider ' + provider + ' not supported.');
};
};

View File

@ -16,6 +16,15 @@ function Insight(opts) {
this.url = opts.url;
};
var _parseErr = function(err, res) {
if (err) {
return "Insight Error";
}
log.warn("Insight " + res.request.href + " Returned Status: " + res.statusCode);
return "Error querying the blockchain";
};
Insight.prototype.getConnectionInfo = function() {
return 'Insight (' + this.network + ') @ ' + this.url;
};
@ -34,7 +43,7 @@ Insight.prototype.getUnspentUtxos = function(addresses, cb) {
};
request(args, function(err, res, unspent) {
if (err || res.statusCode !== 200) return cb(err || res);
if (err || res.statusCode !== 200) return cb(_parseErr(err,res));
return cb(null, unspent);
});
};
@ -53,7 +62,7 @@ Insight.prototype.broadcast = function(rawTx, cb) {
};
request(args, function(err, res, body) {
if (err || res.statusCode !== 200) return cb(err || res);
if (err || res.statusCode !== 200) return cb(_parseErr(err,res));
return cb(null, body ? body.txid : null);
});
};
@ -66,7 +75,7 @@ Insight.prototype.getTransaction = function(txid, cb) {
};
request(args, function(err, res, tx) {
if (err || res.statusCode != 200) return cb(err || res);
if (err || res.statusCode !== 200) return cb(_parseErr(err,res));
return cb(null, tx);
});
};
@ -86,7 +95,7 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
};
request(args, function(err, res, txs) {
if (err || res.statusCode != 200) return cb(err || res);
if (err || res.statusCode !== 200) return cb(_parseErr(err,res));
if (_.isObject(txs) && txs.items)
txs = txs.items;
@ -117,7 +126,7 @@ Insight.prototype.estimateFee = function(nbBlocks, cb) {
json: true,
};
request(args, function(err, res, body) {
if (err || res.statusCode !== 200) return cb(err || res);
if (err || res.statusCode !== 200) return cb(_parseErr(err,res));
return cb(null, body);
});
};

View File

@ -78,15 +78,16 @@ ExpressApp.prototype.start = function(opts, cb) {
message: err.message,
}).end();
} else {
var code, message;
var code = 500, message;
if (_.isObject(err)) {
code = err.code || err.statusCode;
message = err.message || err.body;
}
var m = message || err.toString();
if (!opts.disableLogs)
log.error('Err: ' + req.url + ' :' + code + ':' + m);
log.error(req.url + ' :' + code + ':' + m);
res.status(code || 500).json({
error: m,