diff --git a/app/utils.js b/app/utils.js index 9aa50be..6b9598b 100644 --- a/app/utils.js +++ b/app/utils.js @@ -1,3 +1,8 @@ +var debug = require("debug"); + +var debugLog = debug("btcexp:utils"); +var debugErrorLog = debug("btcexp:error"); + var Decimal = require("decimal.js"); var request = require("request"); var qrcode = require("qrcode"); @@ -303,7 +308,7 @@ function getTxTotalInputOutputValues(tx, txInputs, blockHeight) { totalInputValue = totalInputValue.plus(new Decimal(vout.value)); } } catch (err) { - console.log("Error getting tx.totalInputValue: err=" + err + ", txid=" + tx.txid + ", index=tx.vin[" + i + "]"); + utils.logError("2397gs0gsse", err, {txid:tx.txid, vinIndex:i}); } } } @@ -313,7 +318,7 @@ function getTxTotalInputOutputValues(tx, txInputs, blockHeight) { totalOutputValue = totalOutputValue.plus(new Decimal(tx.vout[i].value)); } } catch (err) { - console.log("Error computing total input/output values for tx: err=" + err + ", tx=" + JSON.stringify(tx) + ", txInputs=" + JSON.stringify(txInputs) + ", blockHeight=" + blockHeight); + utils.logError("2308sh0sg44", err, {tx:tx, txInputs:txInputs, blockHeight:blockHeight}); } return {input:totalInputValue, output:totalOutputValue}; @@ -368,13 +373,13 @@ function refreshExchangeRates() { }); } - console.log("Using exchange rates: " + JSON.stringify(global.exchangeRates) + " starting at " + global.exchangeRatesUpdateTime); + debugLog("Using exchange rates: " + JSON.stringify(global.exchangeRates) + " starting at " + global.exchangeRatesUpdateTime); } else { - console.log("Unable to get exchange rate data"); + debugLog("Unable to get exchange rate data"); } } else { - console.log(`Error 39r7h2390fgewfgds: ${error}, StatusCode: ${(response != null) ? response.statusCode : ""}, Response: ${JSON.stringify(response)}`); + utils.logError("39r7h2390fgewfgds", {error:error, response:response, body:body}); } }); } @@ -400,7 +405,7 @@ function geoLocateIpAddresses(ipAddresses, provider) { if (result.value == null) { var apiUrl = "http://api.ipstack.com/" + result.key + "?access_key=" + config.credentials.ipStackComApiAccessKey; - console.log("Requesting IP-geo: " + apiUrl); + debugLog("Requesting IP-geo: " + apiUrl); request(apiUrl, function(error, response, body) { if (error) { @@ -438,7 +443,7 @@ function geoLocateIpAddresses(ipAddresses, provider) { resolve(ipDetails); }).catch(function(err) { - console.log("Error 80342hrf78wgehdf07gds: " + err); + utils.logError("80342hrf78wgehdf07gds", err); reject(err); }); @@ -507,7 +512,27 @@ function colorHexToHsl(hex) { } function logError(errorId, err, optionalUserData = null) { - console.log("Error " + errorId + ": " + err + ", json: " + JSON.stringify(err) + (optionalUserData != null ? (", userData: " + optionalUserData) : "")); + if (!global.errorLog) { + global.errorLog = []; + } + + global.errorLog.push({errorId:errorId, error:err, userData:optionalUserData, date:new Date()}); + while (global.errorLog.length > 100) { + global.errorLog.splice(0, 1); + } + + debugErrorLog("Error " + errorId + ": " + err + ", json: " + JSON.stringify(err) + (optionalUserData != null ? (", userData: " + optionalUserData + " (json: " + JSON.stringify(optionalUserData) + ")") : "")); + + if (err && err.stack) { + debugErrorLog("Stack: " + err.stack); + } + + var returnVal = {errorId:errorId, error:err}; + if (optionalUserData) { + returnVal.userData = optionalUserData; + } + + return returnVal; } function buildQrCodeUrls(strings) { diff --git a/routes/baseActionsRouter.js b/routes/baseActionsRouter.js index 62038de..9cd6e89 100644 --- a/routes/baseActionsRouter.js +++ b/routes/baseActionsRouter.js @@ -608,13 +608,13 @@ router.get("/address/:address", function(req, res, next) { res.locals.addressObj = bitcoinjs.address.fromBase58Check(address); } catch (err) { - console.log("Error u3gr02gwef: " + err); + utils.logError("u3gr02gwef", err); try { res.locals.addressObj = bitcoinjs.address.fromBech32(address); } catch (err2) { - console.log("Error u02qg02yqge: " + err2); + utils.logError("u02qg02yqge", err); } } @@ -754,8 +754,8 @@ router.get("/address/:address", function(req, res, next) { }).catch(function(err) { res.locals.electrumHistoryError = err; + utils.logError("23t07ug2wghefud", err); - console.log("Error 23t07ug2wghefud: " + err + ", error json: " + JSON.stringify(err)); reject(err); }); @@ -768,7 +768,7 @@ router.get("/address/:address", function(req, res, next) { resolve(); }).catch(function(err) { - console.log("Error 132r80h32rh: " + err + ", error json: " + JSON.stringify(err)); + utils.logError("132r80h32rh", err); reject(err); }); @@ -778,7 +778,7 @@ router.get("/address/:address", function(req, res, next) { promises.push(new Promise(function(resolve, reject) { qrcode.toDataURL(address, function(err, url) { if (err) { - console.log("Error 93ygfew0ygf2gf2: " + err); + utils.logError("93ygfew0ygf2gf2", err); } res.locals.addressQrCodeUrl = url; @@ -793,7 +793,7 @@ router.get("/address/:address", function(req, res, next) { next(); }).catch(function(err) { - console.log("Error 32197rgh327g2: " + err + ", error json: " + JSON.stringify(err)); + utils.logError("32197rgh327g2", err); res.render("address"); @@ -801,6 +801,8 @@ router.get("/address/:address", function(req, res, next) { }); }).catch(function(err) { + utils.logError("2108hs0gsdfe", err, {address:address}); + res.locals.userMessage = "Failed to load address " + address + " (" + err + ")"; res.render("address");