From fdb3e35864c2f4ce52c3d6d273ffd3f9b087cd9a Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 8 Sep 2015 00:57:59 -0300 Subject: [PATCH] better insight error handling --- lib/blockchainexplorer.js | 2 +- lib/blockchainexplorers/insight.js | 19 ++++++++++++++----- lib/expressapp.js | 5 +++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/blockchainexplorer.js b/lib/blockchainexplorer.js index bfffe0e..227dedb 100644 --- a/lib/blockchainexplorer.js +++ b/lib/blockchainexplorer.js @@ -32,7 +32,7 @@ function BlockChainExplorer(opts) { url: url }); default: - throw new Error('Provider ' + provider + ' not supperted.'); + throw new Error('Provider ' + provider + ' not supported.'); }; }; diff --git a/lib/blockchainexplorers/insight.js b/lib/blockchainexplorers/insight.js index 9eb9d67..bde2818 100644 --- a/lib/blockchainexplorers/insight.js +++ b/lib/blockchainexplorers/insight.js @@ -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); }); }; diff --git a/lib/expressapp.js b/lib/expressapp.js index 8eae3b4..de577c4 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -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,