improve handling of ratchet error

This commit is contained in:
Josh Harvey 2018-01-25 20:15:22 +02:00
parent fc17d2cdf8
commit d7a5734818
4 changed files with 6 additions and 3 deletions

View file

@ -76,6 +76,7 @@ function ensureRatchet (oldField, newField, fieldKey) {
const free = ['sendPending', 'error', 'errorCode', 'customerId'] const free = ['sendPending', 'error', 'errorCode', 'customerId']
if (_.isNil(oldField)) return true if (_.isNil(oldField)) return true
if (_.includes(fieldKey, monotonic)) return isMonotonic(oldField, newField, fieldKey) if (_.includes(fieldKey, monotonic)) return isMonotonic(oldField, newField, fieldKey)
if (_.includes(fieldKey, free)) { if (_.includes(fieldKey, free)) {
@ -106,7 +107,7 @@ function diff (oldTx, newTx) {
logger.warn('Value from lamassu-machine would violate ratchet [%s]', fieldKey) logger.warn('Value from lamassu-machine would violate ratchet [%s]', fieldKey)
logger.warn('Old tx: %j', oldTx) logger.warn('Old tx: %j', oldTx)
logger.warn('New tx: %j', newTx) logger.warn('New tx: %j', newTx)
throw new Error('Value from lamassu-machine would violate ratchet') throw new E.RatchetError('Value from lamassu-machine would violate ratchet')
} }
updatedTx[fieldKey] = newField updatedTx[fieldKey] = newField

View file

@ -245,7 +245,6 @@ function preProcess (oldTx, newTx, pi) {
const hasDispenseOccurred = !dispenseOccurred(oldTx.bills) && dispenseOccurred(newTx.bills) const hasDispenseOccurred = !dispenseOccurred(oldTx.bills) && dispenseOccurred(newTx.bills)
if (hasError || hasDispenseOccurred) { if (hasError || hasDispenseOccurred) {
console.log('DEBUG100')
return logDispense(updatedTx) return logDispense(updatedTx)
.then(updateCassettes(updatedTx)) .then(updateCassettes(updatedTx))
} }

View file

@ -23,3 +23,4 @@ register('BadNumberError')
register('NoDataError') register('NoDataError')
register('InsufficientFundsError') register('InsufficientFundsError')
register('StaleTxError') register('StaleTxError')
register('RatchetError')

View file

@ -123,7 +123,9 @@ function postTx (req, res, next) {
return res.json(tx) return res.json(tx)
}) })
.catch(err => { .catch(err => {
if (err instanceof E.StaleTxError) return res.status(404).json({}) if (err instanceof E.StaleTxError) return res.status(409).json({})
if (err instanceof E.RatchetError) return res.status(409).json({})
throw err throw err
}) })
.catch(next) .catch(next)