From 1a8ef31d3990ead911dcbd8cee1efa060023f795 Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Sun, 16 Sep 2018 03:03:08 -0300 Subject: [PATCH] Add db error consts file (#165) * Add db error consts file * Fixed typo in comment --- lib/cash-out/cash-out-tx.js | 7 +++---- lib/db-error-codes.js | 4 ++++ lib/routes.js | 9 +++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 lib/db-error-codes.js diff --git a/lib/cash-out/cash-out-tx.js b/lib/cash-out/cash-out-tx.js index 925fdf35..310e9581 100644 --- a/lib/cash-out/cash-out-tx.js +++ b/lib/cash-out/cash-out-tx.js @@ -3,6 +3,7 @@ const pgp = require('pg-promise')() const pEachSeries = require('p-each-series') const db = require('../db') +const dbErrorCodes = require('../db-error-codes') const billMath = require('../bill-math') const T = require('../time') const logger = require('../logger') @@ -26,8 +27,6 @@ const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes const MAX_NOTIFY_AGE = T.day const MIN_NOTIFY_AGE = 5 * T.minutes 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 @@ -125,8 +124,8 @@ function monitorIncoming (settings, statuses, fromAge, toAge) { return fetchOpenTxs(statuses, fromAge, toAge) .then(txs => pEachSeries(txs, tx => processTxStatus(tx, settings))) .catch(err => { - if (err.code === SERIALIZATION_FAILURE_CODE) { - logger.warn(HARMLESS_DB_CONFLICT_ERROR) + if (err.code === dbErrorCodes.SERIALIZATION_FAILURE) { + logger.warn('Harmless DB conflict, the query will be retried.') } else { logger.error(err) } diff --git a/lib/db-error-codes.js b/lib/db-error-codes.js new file mode 100644 index 00000000..aaa6340b --- /dev/null +++ b/lib/db-error-codes.js @@ -0,0 +1,4 @@ +const dbErrorCodes = { + SERIALIZATION_FAILURE: '40001', +} +module.exports = dbErrorCodes diff --git a/lib/routes.js b/lib/routes.js index d71fe074..914b848b 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -7,6 +7,7 @@ const _ = require('lodash/fp') const express = require('express') const nmd = require('nano-markdown') +const dbErrorCodes = require('./db-error-codes') const options = require('./options') const logger = require('./logger') const configManager = require('./config-manager') @@ -25,7 +26,6 @@ const argv = require('minimist')(process.argv.slice(2)) const CLOCK_SKEW = 60 * 1000 const REQUEST_TTL = 3 * 60 * 1000 -const SERIALIZATION_FAILURE_CODE = '40001' const pids = {} const reboots = {} @@ -135,7 +135,12 @@ function postTx (req, res, next) { return res.json(tx) }) .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.RatchetError) return res.status(409).json({})