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) {
if (!SUPPORTED_COINS.includes(cryptoCode)) {
@ -21,34 +21,35 @@ function checkCryptoCode (cryptoCode) {
}
function balance (account, cryptoCode, settings, operatorId) {
return axios.post('/balance', {
account,
cryptoCode,
settings,
operatorId
}).catch(console.error)
return checkCryptoCode(cryptoCode).then(() => {
return axios.post('/balance', {
account,
cryptoCode,
settings,
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) {
const { cryptoCode } = tx
try {
return checkCryptoCode(cryptoCode).then(() => {
return axios.post('/sendCoins', {
account,
tx,
settings,
operatorId
}).then(({ data }) => {
if (data.error && data.error.message === 'insufficient funds') throw new E.InsufficientFundsError()
else if (data.error) throw new Error({ error: data.error, message: data.error.message })
const fee = BN(data.fee).round()
const txid = data.txid
return { txid, fee }
})
return checkCryptoCode(cryptoCode).then(() => {
return axios.post('/sendCoins', {
account,
tx,
settings,
operatorId
}).then(({ data }) => {
if (data.error && data.error.errorCode === 'sc-001') throw new E.InsufficientFundsError()
else if (data.error) throw new Error(JSON.stringify({ errorCode: data.error.errorCode, message: data.error.message }))
const fee = BN(data.fee).round()
const txid = data.txid
return { txid, fee }
})
} catch (e) {
throw e
}
})
}
function newAddress (account, info, tx, settings, operatorId) {
@ -91,12 +92,18 @@ function sweep (account, cryptoCode, hdIndex, settings, operatorId) {
}
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return axios.post('/cryptoNetwork', {
account,
cryptoCode,
settings,
operatorId
}).catch(console.error)
return checkCryptoCode(cryptoCode)
.then(() => axios.post('/cryptoNetwork', {
account,
cryptoCode,
settings,
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) {