Add db error consts file (#165)

* Add db error consts file

* Fixed typo in comment
This commit is contained in:
Rafael Taranto 2018-09-16 03:03:08 -03:00 committed by Josh Harvey
parent 7c4c314df4
commit 1a8ef31d39
3 changed files with 14 additions and 6 deletions

View file

@ -3,6 +3,7 @@ const pgp = require('pg-promise')()
const pEachSeries = require('p-each-series') const pEachSeries = require('p-each-series')
const db = require('../db') const db = require('../db')
const dbErrorCodes = require('../db-error-codes')
const billMath = require('../bill-math') const billMath = require('../bill-math')
const T = require('../time') const T = require('../time')
const logger = require('../logger') const logger = require('../logger')
@ -26,8 +27,6 @@ const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
const MAX_NOTIFY_AGE = T.day const MAX_NOTIFY_AGE = T.day
const MIN_NOTIFY_AGE = 5 * T.minutes const MIN_NOTIFY_AGE = 5 * T.minutes
const INSUFFICIENT_FUNDS_CODE = 570 const INSUFFICIENT_FUNDS_CODE = 570
const SERIALIZATION_FAILURE_CODE = '40001'
const HARMLESS_DB_CONFLICT_ERROR = 'Harmless DB conflict, the query will be retried.'
const toObj = helper.toObj const toObj = helper.toObj
@ -125,8 +124,8 @@ function monitorIncoming (settings, statuses, fromAge, toAge) {
return fetchOpenTxs(statuses, fromAge, toAge) return fetchOpenTxs(statuses, fromAge, toAge)
.then(txs => pEachSeries(txs, tx => processTxStatus(tx, settings))) .then(txs => pEachSeries(txs, tx => processTxStatus(tx, settings)))
.catch(err => { .catch(err => {
if (err.code === SERIALIZATION_FAILURE_CODE) { if (err.code === dbErrorCodes.SERIALIZATION_FAILURE) {
logger.warn(HARMLESS_DB_CONFLICT_ERROR) logger.warn('Harmless DB conflict, the query will be retried.')
} else { } else {
logger.error(err) logger.error(err)
} }

4
lib/db-error-codes.js Normal file
View file

@ -0,0 +1,4 @@
const dbErrorCodes = {
SERIALIZATION_FAILURE: '40001',
}
module.exports = dbErrorCodes

View file

@ -7,6 +7,7 @@ const _ = require('lodash/fp')
const express = require('express') const express = require('express')
const nmd = require('nano-markdown') const nmd = require('nano-markdown')
const dbErrorCodes = require('./db-error-codes')
const options = require('./options') const options = require('./options')
const logger = require('./logger') const logger = require('./logger')
const configManager = require('./config-manager') const configManager = require('./config-manager')
@ -25,7 +26,6 @@ const argv = require('minimist')(process.argv.slice(2))
const CLOCK_SKEW = 60 * 1000 const CLOCK_SKEW = 60 * 1000
const REQUEST_TTL = 3 * 60 * 1000 const REQUEST_TTL = 3 * 60 * 1000
const SERIALIZATION_FAILURE_CODE = '40001'
const pids = {} const pids = {}
const reboots = {} const reboots = {}
@ -135,7 +135,12 @@ function postTx (req, res, next) {
return res.json(tx) return res.json(tx)
}) })
.catch(err => { .catch(err => {
if (err.code === SERIALIZATION_FAILURE_CODE) return res.status(204).json({}) // 204 so that l-m can ignore the error
// this is fine because the request is polled and will be retried if needed.
if (err.code === dbErrorCodes.SERIALIZATION_FAILURE) {
logger.warn('Harmless DB conflict, the query will be retried.')
return res.status(204).json({})
}
if (err instanceof E.StaleTxError) return res.status(409).json({}) if (err instanceof E.StaleTxError) return res.status(409).json({})
if (err instanceof E.RatchetError) return res.status(409).json({}) if (err instanceof E.RatchetError) return res.status(409).json({})