Fix: error handling

This commit is contained in:
csrapr 2021-03-25 16:44:44 +00:00 committed by Josh Harvey
parent 800d09dd71
commit df37bcc519

View file

@ -10,7 +10,7 @@ const axios = require('axios').create({
}) })
}) })
const SUPPORTED_COINS = ['BTC'] const SUPPORTED_COINS = ['BTC', 'ZEC', 'LTC', 'BCH', 'DASH', 'ETH']
function checkCryptoCode (cryptoCode) { function checkCryptoCode (cryptoCode) {
if (!SUPPORTED_COINS.includes(cryptoCode)) { if (!SUPPORTED_COINS.includes(cryptoCode)) {
@ -21,34 +21,35 @@ function checkCryptoCode (cryptoCode) {
} }
function balance (account, cryptoCode, settings, operatorId) { function balance (account, cryptoCode, settings, operatorId) {
return axios.post('/balance', { return checkCryptoCode(cryptoCode).then(() => {
account, return axios.post('/balance', {
cryptoCode, account,
settings, cryptoCode,
operatorId settings,
}).catch(console.error) operatorId
}).then(({ data }) => {
if (data.error) throw new Error(JSON.stringify({ errorCode: data.error.errorCode, message: data.error.message }))
return BN(data.balance)
})
})
} }
function sendCoins (account, tx, settings, operatorId) { function sendCoins (account, tx, settings, operatorId) {
const { cryptoCode } = tx const { cryptoCode } = tx
try { return checkCryptoCode(cryptoCode).then(() => {
return checkCryptoCode(cryptoCode).then(() => { return axios.post('/sendCoins', {
return axios.post('/sendCoins', { account,
account, tx,
tx, settings,
settings, operatorId
operatorId }).then(({ data }) => {
}).then(({ data }) => { if (data.error && data.error.errorCode === 'sc-001') throw new E.InsufficientFundsError()
if (data.error && data.error.message === 'insufficient funds') throw new E.InsufficientFundsError() else if (data.error) throw new Error(JSON.stringify({ errorCode: data.error.errorCode, message: data.error.message }))
else if (data.error) throw new Error({ error: data.error, message: data.error.message }) const fee = BN(data.fee).round()
const fee = BN(data.fee).round() const txid = data.txid
const txid = data.txid return { txid, fee }
return { txid, fee }
})
}) })
} catch (e) { })
throw e
}
} }
function newAddress (account, info, tx, settings, operatorId) { function newAddress (account, info, tx, settings, operatorId) {
@ -91,12 +92,18 @@ function sweep (account, cryptoCode, hdIndex, settings, operatorId) {
} }
function cryptoNetwork (account, cryptoCode, settings, operatorId) { function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return axios.post('/cryptoNetwork', { return checkCryptoCode(cryptoCode)
account, .then(() => axios.post('/cryptoNetwork', {
cryptoCode, account,
settings, cryptoCode,
operatorId settings,
}).catch(console.error) operatorId
}))
.then(({ data }) => {
if (data.error && data.error.errorCode === 'cn-001') return false
else if (data.error) throw new Error(JSON.stringify({ error: data.error, message: data.error.message }))
return data.cryptoNetwork
})
} }
function isStrictAddress (cryptoCode, toAddress, settings, operatorId) { function isStrictAddress (cryptoCode, toAddress, settings, operatorId) {