From 9f43336a036ef9d19ea9650d1d4f7b17e679172f Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Sun, 19 Jan 2025 20:14:03 -0800 Subject: [PATCH] Use response handler in handleErrors middleare --- Server/controllers/authController.js | 5 ++++- Server/controllers/settingsController.js | 2 +- Server/middleware/handleErrors.js | 5 ++++- Server/middleware/responseHandler.js | 4 ++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 319558b92..0ffa612f3 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -153,7 +153,9 @@ class AuthController { // Compare password const match = await user.comparePassword(password); if (match !== true) { - next(new Error(errorMessages.AUTH_INCORRECT_PASSWORD)); + const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD); + error.status = 401; + next(error); return; } @@ -182,6 +184,7 @@ class AuthController { }, }); } catch (error) { + error.status = 401; next(handleError(error, SERVICE_NAME, "loginUser")); } }; diff --git a/Server/controllers/settingsController.js b/Server/controllers/settingsController.js index 00b99120c..454dd48e8 100644 --- a/Server/controllers/settingsController.js +++ b/Server/controllers/settingsController.js @@ -34,7 +34,7 @@ class SettingsController { await this.db.updateAppSettings(req.body); const updatedSettings = { ...(await this.settingsService.reloadSettings()) }; delete updatedSettings.jwtSecret; - return res.successs({ + return res.success({ msg: successMessages.UPDATE_APP_SETTINGS, data: updatedSettings, }); diff --git a/Server/middleware/handleErrors.js b/Server/middleware/handleErrors.js index bff2341c3..fa9af4c0b 100644 --- a/Server/middleware/handleErrors.js +++ b/Server/middleware/handleErrors.js @@ -11,7 +11,10 @@ const handleErrors = (error, req, res, next) => { method: error.method, stack: error.stack, }); - res.status(status).json({ success: false, msg: message }); + res.error({ + status, + msg: message, + }); }; export { handleErrors }; diff --git a/Server/middleware/responseHandler.js b/Server/middleware/responseHandler.js index 7f6f28986..0088aa3c6 100644 --- a/Server/middleware/responseHandler.js +++ b/Server/middleware/responseHandler.js @@ -7,8 +7,8 @@ const responseHandler = (req, res, next) => { }); }; - res.error = (status = 500, msg = "Internal server erro", data = null) => { - res.status(status).json({ + res.error = ({ status = 500, msg = "Internal server erro", data = null }) => { + return res.status(status).json({ success: false, msg, data,