Fix: error handling
This commit is contained in:
parent
800d09dd71
commit
df37bcc519
1 changed files with 37 additions and 30 deletions
|
|
@ -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,17 +21,21 @@ function checkCryptoCode (cryptoCode) {
|
|||
}
|
||||
|
||||
function balance (account, cryptoCode, settings, operatorId) {
|
||||
return checkCryptoCode(cryptoCode).then(() => {
|
||||
return axios.post('/balance', {
|
||||
account,
|
||||
cryptoCode,
|
||||
settings,
|
||||
operatorId
|
||||
}).catch(console.error)
|
||||
}).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,
|
||||
|
|
@ -39,16 +43,13 @@ function sendCoins (account, tx, settings, operatorId) {
|
|||
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 })
|
||||
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', {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => axios.post('/cryptoNetwork', {
|
||||
account,
|
||||
cryptoCode,
|
||||
settings,
|
||||
operatorId
|
||||
}).catch(console.error)
|
||||
}))
|
||||
.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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue