From b47af59a7fc36c1b14a80349d5f41002e47e41f7 Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Sat, 27 Oct 2018 09:22:36 -0300 Subject: [PATCH] Improve transaction errors (#194) * Improve transaction errors * Fix standard styling errors --- lib/routes.js | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/routes.js b/lib/routes.js index 10c7af87..3f8e81dd 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -46,7 +46,7 @@ function poll (req, res, next) { const pi = plugins(settings, deviceId) const hasLightning = checkHasLightning(settings) - pids[deviceId] = {pid, ts: Date.now()} + pids[deviceId] = { pid, ts: Date.now() } return pi.pollQueries(serialNumber, deviceTime, req.query) .then(results => { @@ -141,8 +141,8 @@ function postTx (req, res, next) { 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({}) + if (err instanceof E.StaleTxError) return res.status(409).json({ errorType: 'stale' }) + if (err instanceof E.RatchetError) return res.status(409).json({ errorType: 'ratchet' }) throw err }) @@ -202,7 +202,7 @@ function getCustomerWithPhoneCode (req, res, next) { return pi.getPhoneCode(phone) .then(code => { return addOrUpdateCustomer(req) - .then(customer => respond(req, res, {code, customer})) + .then(customer => respond(req, res, { code, customer })) }) .catch(err => { 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.update(id, newPatch)) }) - .then(customer => respond(req, res, {customer})) + .then(customer => respond(req, res, { customer })) .catch(next) } @@ -238,7 +238,7 @@ function getLastSeen (req, res, next) { function updateLogs (req, res, next) { return logs.update(req.deviceId, req.body.logs) - .then(status => res.json({success: status})) + .then(status => res.json({ success: status })) .catch(next) } @@ -246,8 +246,8 @@ function ca (req, res) { const token = req.query.token return pairing.authorizeCaDownload(token) - .then(ca => res.json({ca})) - .catch(() => res.status(403).json({error: 'forbidden'})) + .then(ca => res.json({ ca })) + .catch(() => res.status(403).json({ error: 'forbidden' })) } function pair (req, res, next) { @@ -259,7 +259,7 @@ function pair (req, res, next) { .then(valid => { if (valid) { return helpers.updateMachineDefaults(deviceId) - .then(() => res.json({status: 'paired'})) + .then(() => res.json({ status: 'paired' })) } throw httpError('Pairing failed') @@ -272,7 +272,7 @@ function errorHandler (err, req, res, next) { ? err.code || 500 : 500 - const json = {error: err.message} + const json = { error: err.message } if (statusCode >= 400) logger.error(err) @@ -303,7 +303,7 @@ function filterOldRequests (req, res, next) { 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() } @@ -318,7 +318,7 @@ function authorize (req, res, next) { return next() } - return res.status(403).json({error: 'Forbidden'}) + return res.status(403).json({ error: 'Forbidden' }) }) .catch(next) } @@ -337,9 +337,9 @@ const configRequiredRoutes = [ const app = express() const localApp = express() -app.use(helmet({noCache: true})) -app.use(bodyParser.json({limit: '2mb'})) -app.use(morgan('dev', {skip, stream: logger.stream})) +app.use(helmet({ noCache: true })) +app.use(bodyParser.json({ limit: '2mb' })) +app.use(morgan('dev', { skip, stream: logger.stream })) // These two have their own authorization app.post('/pair', populateDeviceId, pair) @@ -368,7 +368,7 @@ app.post('/logs', updateLogs) app.use(errorHandler) 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) => { @@ -440,14 +440,14 @@ function populateSettings (req, res, next) { .catch(next) } -function createTerms(config) { - return { - active: config.termsScreenActive, - title: config.termsScreenTitle, - text: nmd(config.termsScreenText), - accept: config.termsAcceptButtonText, - cancel: config.termsCancelButtonText - } +function createTerms (config) { + return { + active: config.termsScreenActive, + title: config.termsScreenTitle, + text: nmd(config.termsScreenText), + accept: config.termsAcceptButtonText, + cancel: config.termsCancelButtonText + } } -module.exports = {app, localApp} +module.exports = { app, localApp }