From de70aa29fd008b0d633050f8bc4128189fd499f6 Mon Sep 17 00:00:00 2001 From: Jakob Date: Sat, 14 Dec 2019 06:14:13 -0800 Subject: [PATCH] Use constants instead of number literals for http status codes (#5276) --- lib/api/notifications-api.js | 8 +++++--- lib/api/notifications-v2.js | 6 ++++-- lib/api3/index.js | 6 +++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/api/notifications-api.js b/lib/api/notifications-api.js index f9810256ad8..d08a03a7ead 100644 --- a/lib/api/notifications-api.js +++ b/lib/api/notifications-api.js @@ -1,5 +1,7 @@ 'use strict'; +var consts = require('../constants'); + function configure (app, wares, ctx) { var express = require('express') , api = express.Router( ) @@ -7,9 +9,9 @@ function configure (app, wares, ctx) { api.post('/notifications/pushovercallback', function (req, res) { if (ctx.pushnotify.pushoverAck(req.body)) { - res.sendStatus(200); + res.sendStatus(consts.HTTP_OK); } else { - res.sendStatus(500); + res.sendStatus(consts.HTTP_INTERNAL_ERROR); } }); @@ -21,7 +23,7 @@ function configure (app, wares, ctx) { var time = req.query.time && Number(req.query.time); console.info('got api ack, level: ', level, ', time: ', time, ', query: ', req.query); ctx.notifications.ack(level, group, time, true); - res.sendStatus(200); + res.sendStatus(consts.HTTP_OK); }); } diff --git a/lib/api/notifications-v2.js b/lib/api/notifications-v2.js index 38bc73e4a78..16eac1de975 100644 --- a/lib/api/notifications-v2.js +++ b/lib/api/notifications-v2.js @@ -1,5 +1,7 @@ 'use strict'; +var consts = require('../constants'); + function configure (app, ctx) { var express = require('express') , api = express.Router( ) @@ -8,10 +10,10 @@ function configure (app, ctx) { api.post('/loop', ctx.authorization.isPermitted('notifications:loop:push'), function (req, res) { ctx.loop.sendNotification(req.body, req.connection.remoteAddress, function (error) { if (error) { - res.status(500).send(error) + res.status(consts.HTTP_INTERNAL_ERROR).send(error) console.log("error sending notification to Loop: ", error); } else { - res.sendStatus(200); + res.sendStatus(consts.HTTP_OK); } }); }); diff --git a/lib/api3/index.js b/lib/api3/index.js index d188a5ee537..5b1799c78fd 100644 --- a/lib/api3/index.js +++ b/lib/api3/index.js @@ -39,8 +39,8 @@ function configure (env, ctx) { limit: 1048576 * 50 }), function errorHandler (err, req, res, next) { console.error(err); - res.status(500).json({ - status: 500, + res.status(apiConst.HTTP.INTERNAL_ERROR).json({ + status: apiConst.HTTP.INTERNAL_ERROR, message: apiConst.MSG.HTTP_500_INTERNAL_ERROR }); if (next) { // we need 4th parameter next to behave like error handler, but we have to use it to prevent "unused variable" message @@ -81,7 +81,7 @@ function configure (env, ctx) { const opCtx = {app, ctx, env, req, res}; opCtx.auth = await security.authenticate(opCtx); await security.demandPermission(opCtx, 'api:entries:read'); - res.status(200).end(); + res.status(apiConst.HTTP.OK).end(); } catch (error) { console.error(error); }