handle cash-in errors

This commit is contained in:
Josh Harvey 2017-04-23 13:44:36 +03:00
parent 48dd23c11d
commit 86adfd0a63
7 changed files with 40 additions and 24 deletions

View file

@ -174,6 +174,11 @@ function postProcess (txVector, pi) {
sendConfirmed: true, sendConfirmed: true,
sendTime: 'now()^' sendTime: 'now()^'
})) }))
.catch(err => ({
sendTime: 'now()^',
error: err.message,
errorCode: err.name
}))
} }
return Promise.resolve({}) return Promise.resolve({})

View file

@ -1,11 +1,14 @@
const E = function generateError (name) { const _ = require('lodash/fp')
const E = function (name) {
var CustomErr = function (msg) { var CustomErr = function (msg) {
this.message = msg this.message = msg || _.startCase(name)
this.name = name this.name = name
Error.captureStackTrace(this, CustomErr) Error.captureStackTrace(this, CustomErr)
} }
CustomErr.prototype = Object.create(Error.prototype) CustomErr.prototype = Object.create(Error.prototype)
CustomErr.prototype.constructor = CustomErr CustomErr.prototype.constructor = CustomErr
CustomErr.code = name
return CustomErr return CustomErr
} }
@ -18,3 +21,4 @@ function register (errorName) {
register('BadNumberError') register('BadNumberError')
register('NoDataError') register('NoDataError')
register('InsufficientFundsError')

View file

@ -5,6 +5,7 @@ const fs = require('fs')
const pify = require('pify') const pify = require('pify')
const BN = require('../../../bn') const BN = require('../../../bn')
const E = require('../../../error')
const NAME = 'Bitcoind' const NAME = 'Bitcoind'
@ -28,12 +29,6 @@ function initRpc () {
return new RpcClient(rpcConfig) return new RpcClient(rpcConfig)
} }
function richError (msg, name) {
const err = new Error(msg)
err.name = name
return err
}
/* /*
* initialize RpcClient * initialize RpcClient
*/ */
@ -68,7 +63,7 @@ function balance (account, cryptoCode) {
if (err) return reject(err) if (err) return reject(err)
if (result.error) { if (result.error) {
return reject(richError(result.error, 'bitcoindError')) return reject(err)
} }
resolve(BN(result.result).shift(SATOSHI_SHIFT).round()) resolve(BN(result.result).shift(SATOSHI_SHIFT).round())
@ -85,15 +80,8 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
checkCryptoCode(cryptoCode) checkCryptoCode(cryptoCode)
rpc.sendFrom(pluginConfig.account, address, bitcoins, confirmations, (err, result) => { rpc.sendFrom(pluginConfig.account, address, bitcoins, confirmations, (err, result) => {
if (err) { if (err) {
if (err.code === -6) { if (err.code === -6) return reject(new E.InsufficientFundsError())
return reject(richError('Insufficient funds', 'InsufficientFunds')) return reject(err)
}
if (err instanceof Error) {
return reject(err)
}
return reject(richError(err.message, 'bitcoindError'))
} }
resolve(result.result) resolve(result.result)

View file

@ -1,6 +1,8 @@
const BitGo = require('bitgo') const BitGo = require('bitgo')
const BN = require('../../../bn') const BN = require('../../../bn')
const E = require('../../../error')
const pjson = require('../../../../package.json') const pjson = require('../../../../package.json')
const userAgent = 'Lamassu-Server/' + pjson.version const userAgent = 'Lamassu-Server/' + pjson.version
@ -38,9 +40,7 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
return result.hash return result.hash
}) })
.catch(err => { .catch(err => {
if (err.message === 'Insufficient funds') { if (err.message === 'Insufficient funds') throw new E.InsufficientFundsError()
err.name = 'InsufficientFunds'
}
throw err throw err
}) })
} }

View file

@ -1,4 +1,5 @@
const BN = require('../../../bn') const BN = require('../../../bn')
const E = require('../../../error')
const NAME = 'FakeWallet' const NAME = 'FakeWallet'
@ -18,12 +19,18 @@ function balance (account, cryptoCode) {
}) })
} }
function isInsufficient (cryptoCode) {
if (cryptoCode === 'BTC') return BN(1e5 * 10)
if (cryptoCode === 'ETH') return BN(1e18 * 0.25)
throw new Error('Unsupported crypto: ' + cryptoCode)
}
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) { function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
return new Promise(resolve => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
console.log('[%s] DEBUG: Mock wallet sending %s cryptoAtoms to %s', console.log('[%s] DEBUG: Mock wallet sending %s cryptoAtoms to %s',
cryptoCode, cryptoAtoms.toString(), toAddress) cryptoCode, cryptoAtoms.toString(), toAddress)
resolve('<txHash>') if (isInsufficient(cryptoCode)) return reject(new E.InsufficientFundsError())
return resolve('<txHash>')
}, 2000) }, 2000)
}) })
} }

View file

@ -15,6 +15,7 @@ const plugins = require('./plugins')
const helpers = require('./route-helpers') const helpers = require('./route-helpers')
const poller = require('./poller') const poller = require('./poller')
const Tx = require('./tx') const Tx = require('./tx')
const E = require('./error')
const argv = require('minimist')(process.argv.slice(2)) const argv = require('minimist')(process.argv.slice(2))
@ -102,7 +103,17 @@ function postTx (req, res, next) {
const pi = plugins(req.settings, req.deviceId) const pi = plugins(req.settings, req.deviceId)
return Tx.post(_.set('deviceId', req.deviceId, req.body), pi) return Tx.post(_.set('deviceId', req.deviceId, req.body), pi)
.then(tx => res.json(tx)) .then(tx => {
if (tx.errorCode) {
console.log('DEBUG100: %s, %s', tx.errorCode, E.InsufficientFundsError.name)
if (tx.errorCode === E.InsufficientFundsError.code) {
throw httpError(tx.error, 570)
}
throw httpError(tx.error, 500)
}
return res.json(tx)
})
.catch(next) .catch(next)
} }

View file

@ -6,6 +6,7 @@ exports.up = function (next) {
'alter table cash_in_txs add column device_time bigint not null', 'alter table cash_in_txs add column device_time bigint not null',
'alter table cash_in_txs add column timedout boolean not null default false', 'alter table cash_in_txs add column timedout boolean not null default false',
'alter table cash_in_txs add column send_time timestamptz', 'alter table cash_in_txs add column send_time timestamptz',
'alter table cash_in_txs add column error_code text',
'alter table cash_out_txs add column device_time bigint not null', 'alter table cash_out_txs add column device_time bigint not null',
'alter table cash_out_txs add column timedout boolean not null default false' 'alter table cash_out_txs add column timedout boolean not null default false'
] ]