Browse Source

better insight error handling

activeAddress
Matias Alejo Garcia 9 years ago
parent
commit
fdb3e35864
  1. 2
      lib/blockchainexplorer.js
  2. 19
      lib/blockchainexplorers/insight.js
  3. 5
      lib/expressapp.js

2
lib/blockchainexplorer.js

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

19
lib/blockchainexplorers/insight.js

@ -16,6 +16,15 @@ function Insight(opts) {
this.url = opts.url; 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() { Insight.prototype.getConnectionInfo = function() {
return 'Insight (' + this.network + ') @ ' + this.url; return 'Insight (' + this.network + ') @ ' + this.url;
}; };
@ -34,7 +43,7 @@ Insight.prototype.getUnspentUtxos = function(addresses, cb) {
}; };
request(args, function(err, res, unspent) { 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); return cb(null, unspent);
}); });
}; };
@ -53,7 +62,7 @@ Insight.prototype.broadcast = function(rawTx, cb) {
}; };
request(args, function(err, res, body) { 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); return cb(null, body ? body.txid : null);
}); });
}; };
@ -66,7 +75,7 @@ Insight.prototype.getTransaction = function(txid, cb) {
}; };
request(args, function(err, res, tx) { 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); return cb(null, tx);
}); });
}; };
@ -86,7 +95,7 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
}; };
request(args, function(err, res, txs) { 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) if (_.isObject(txs) && txs.items)
txs = txs.items; txs = txs.items;
@ -117,7 +126,7 @@ Insight.prototype.estimateFee = function(nbBlocks, cb) {
json: true, json: true,
}; };
request(args, function(err, res, body) { 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); return cb(null, body);
}); });
}; };

5
lib/expressapp.js

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

Loading…
Cancel
Save