From 145a21cc8dc78674f7592f4a1741de12a90fd105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Wed, 24 Feb 2021 19:46:43 +0000 Subject: [PATCH] fix: handle errors when wallet is unreachable --- lib/plugins/common/json-rpc.js | 23 +++++++++++++---------- lib/plugins/wallet/zcashd/zcashd.js | 18 ++++++++++++------ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/plugins/common/json-rpc.js b/lib/plugins/common/json-rpc.js index 03cad38f..7d29cd66 100644 --- a/lib/plugins/common/json-rpc.js +++ b/lib/plugins/common/json-rpc.js @@ -48,18 +48,21 @@ function split (str) { } function parseConf (confPath) { - const conf = fs.readFileSync(confPath) - const lines = conf.toString().split('\n') + try { + const conf = fs.readFileSync(confPath) + const lines = conf.toString().split('\n') - const res = {} - for (let i = 0; i < lines.length; i++) { - const keyVal = split(lines[i]) + const res = {} + for (let i = 0; i < lines.length; i++) { + const keyVal = split(lines[i]) - // skip when value is empty - if (!keyVal[1]) continue + // skip when value is empty + if (!keyVal[1]) continue - res[keyVal[0]] = keyVal[1] + res[keyVal[0]] = keyVal[1] + } + return res + } catch (e) { + throw new Error('wallet is currently not installed') } - - return res } diff --git a/lib/plugins/wallet/zcashd/zcashd.js b/lib/plugins/wallet/zcashd/zcashd.js index b5b23da6..a594612b 100644 --- a/lib/plugins/wallet/zcashd/zcashd.js +++ b/lib/plugins/wallet/zcashd/zcashd.js @@ -9,16 +9,22 @@ const E = require('../../../error') const cryptoRec = coinUtils.getCryptoCurrency('ZEC') const configPath = coinUtils.configPath(cryptoRec) const unitScale = cryptoRec.unitScale -const config = jsonRpc.parseConf(configPath) -const rpcConfig = { - username: config.rpcuser, - password: config.rpcpassword, - port: config.rpcport || cryptoRec.defaultPort +function rpcConfig () { + try { + const config = jsonRpc.parseConf(configPath) + return { + username: config.rpcuser, + password: config.rpcpassword, + port: config.rpcport || cryptoRec.defaultPort + } + } catch (err) { + throw err + } } function fetch (method, params) { - return jsonRpc.fetch(rpcConfig, method, params) + return jsonRpc.fetch(rpcConfig(), method, params) } function checkCryptoCode (cryptoCode) {