Improve transaction errors (#194)

* Improve transaction errors

* Fix standard styling errors
This commit is contained in:
Rafael Taranto 2018-10-27 09:22:36 -03:00 committed by Josh Harvey
parent 3cffbedd9a
commit b47af59a7f

View file

@ -46,7 +46,7 @@ function poll (req, res, next) {
const pi = plugins(settings, deviceId) const pi = plugins(settings, deviceId)
const hasLightning = checkHasLightning(settings) const hasLightning = checkHasLightning(settings)
pids[deviceId] = {pid, ts: Date.now()} pids[deviceId] = { pid, ts: Date.now() }
return pi.pollQueries(serialNumber, deviceTime, req.query) return pi.pollQueries(serialNumber, deviceTime, req.query)
.then(results => { .then(results => {
@ -141,8 +141,8 @@ function postTx (req, res, next) {
logger.warn('Harmless DB conflict, the query will be retried.') logger.warn('Harmless DB conflict, the query will be retried.')
return res.status(204).json({}) 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({ errorType: 'stale' })
if (err instanceof E.RatchetError) return res.status(409).json({}) if (err instanceof E.RatchetError) return res.status(409).json({ errorType: 'ratchet' })
throw err throw err
}) })
@ -202,7 +202,7 @@ function getCustomerWithPhoneCode (req, res, next) {
return pi.getPhoneCode(phone) return pi.getPhoneCode(phone)
.then(code => { .then(code => {
return addOrUpdateCustomer(req) return addOrUpdateCustomer(req)
.then(customer => respond(req, res, {code, customer})) .then(customer => respond(req, res, { code, customer }))
}) })
.catch(err => { .catch(err => {
if (err.name === 'BadNumberError') throw httpError('Bad number', 401) if (err.name === 'BadNumberError') throw httpError('Bad number', 401)
@ -226,7 +226,7 @@ function updateCustomer (req, res, next) {
.then(newPatch => customers.updatePhotoCard(id, newPatch)) .then(newPatch => customers.updatePhotoCard(id, newPatch))
.then(newPatch => customers.update(id, newPatch)) .then(newPatch => customers.update(id, newPatch))
}) })
.then(customer => respond(req, res, {customer})) .then(customer => respond(req, res, { customer }))
.catch(next) .catch(next)
} }
@ -238,7 +238,7 @@ function getLastSeen (req, res, next) {
function updateLogs (req, res, next) { function updateLogs (req, res, next) {
return logs.update(req.deviceId, req.body.logs) return logs.update(req.deviceId, req.body.logs)
.then(status => res.json({success: status})) .then(status => res.json({ success: status }))
.catch(next) .catch(next)
} }
@ -246,8 +246,8 @@ function ca (req, res) {
const token = req.query.token const token = req.query.token
return pairing.authorizeCaDownload(token) return pairing.authorizeCaDownload(token)
.then(ca => res.json({ca})) .then(ca => res.json({ ca }))
.catch(() => res.status(403).json({error: 'forbidden'})) .catch(() => res.status(403).json({ error: 'forbidden' }))
} }
function pair (req, res, next) { function pair (req, res, next) {
@ -259,7 +259,7 @@ function pair (req, res, next) {
.then(valid => { .then(valid => {
if (valid) { if (valid) {
return helpers.updateMachineDefaults(deviceId) return helpers.updateMachineDefaults(deviceId)
.then(() => res.json({status: 'paired'})) .then(() => res.json({ status: 'paired' }))
} }
throw httpError('Pairing failed') throw httpError('Pairing failed')
@ -272,7 +272,7 @@ function errorHandler (err, req, res, next) {
? err.code || 500 ? err.code || 500
: 500 : 500
const json = {error: err.message} const json = { error: err.message }
if (statusCode >= 400) logger.error(err) if (statusCode >= 400) logger.error(err)
@ -303,7 +303,7 @@ function filterOldRequests (req, res, next) {
req.deviceName, (delta / 1000).toFixed(2)) req.deviceName, (delta / 1000).toFixed(2))
} }
if (delta > REQUEST_TTL) return res.status(408).json({error: 'stale'}) if (delta > REQUEST_TTL) return res.status(408).json({ error: 'stale' })
next() next()
} }
@ -318,7 +318,7 @@ function authorize (req, res, next) {
return next() return next()
} }
return res.status(403).json({error: 'Forbidden'}) return res.status(403).json({ error: 'Forbidden' })
}) })
.catch(next) .catch(next)
} }
@ -337,9 +337,9 @@ const configRequiredRoutes = [
const app = express() const app = express()
const localApp = express() const localApp = express()
app.use(helmet({noCache: true})) app.use(helmet({ noCache: true }))
app.use(bodyParser.json({limit: '2mb'})) app.use(bodyParser.json({ limit: '2mb' }))
app.use(morgan('dev', {skip, stream: logger.stream})) app.use(morgan('dev', { skip, stream: logger.stream }))
// These two have their own authorization // These two have their own authorization
app.post('/pair', populateDeviceId, pair) app.post('/pair', populateDeviceId, pair)
@ -368,7 +368,7 @@ app.post('/logs', updateLogs)
app.use(errorHandler) app.use(errorHandler)
app.use((req, res) => { app.use((req, res) => {
res.status(404).json({error: 'No such route'}) res.status(404).json({ error: 'No such route' })
}) })
localApp.get('/pid', (req, res) => { localApp.get('/pid', (req, res) => {
@ -440,14 +440,14 @@ function populateSettings (req, res, next) {
.catch(next) .catch(next)
} }
function createTerms(config) { function createTerms (config) {
return { return {
active: config.termsScreenActive, active: config.termsScreenActive,
title: config.termsScreenTitle, title: config.termsScreenTitle,
text: nmd(config.termsScreenText), text: nmd(config.termsScreenText),
accept: config.termsAcceptButtonText, accept: config.termsAcceptButtonText,
cancel: config.termsCancelButtonText cancel: config.termsCancelButtonText
} }
} }
module.exports = {app, localApp} module.exports = { app, localApp }