From be0db003ad72d25e7dae5b4cd62eea1cffaf121c Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Sun, 15 Sep 2024 09:25:35 +0800 Subject: [PATCH] Add service name and method name to errors in authController --- Server/controllers/alertController.js | 5 +-- Server/controllers/authController.js | 46 ++++++++++++++++++++------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/Server/controllers/alertController.js b/Server/controllers/alertController.js index 93d165469..cd31db565 100644 --- a/Server/controllers/alertController.js +++ b/Server/controllers/alertController.js @@ -11,7 +11,7 @@ const { deleteAlertParamValidation, } = require("../validation/joi"); const { successMessages } = require("../utils/messages"); -const SERVICE_NAME = "alerts"; +const SERVICE_NAME = "alertController"; /** * Creates an alert for a monitor @@ -41,7 +41,8 @@ const createAlert = async (req, res, next) => { .status(200) .json({ success: true, msg: successMessages.ALERT_CREATE, data: alert }); } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined ? (error.method = "createAlert") : null; next(error); } }; diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index b3a809a09..a3e245ef3 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -100,7 +100,8 @@ const registerController = async (req, res, next) => { data: { user: newUser, token: token }, }); } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined ? (error.method = "registerController") : null; next(error); } }; @@ -152,7 +153,8 @@ const loginController = async (req, res, next) => { }); } catch (error) { error.status = 500; - // Anything else should be an error + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined ? (error.method = "loginController") : null; next(error); } }; @@ -213,7 +215,8 @@ const userEditController = async (req, res, next) => { data: updatedUser, }); } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined ? (error.method = "userEditController") : null; next(error); return; } @@ -259,7 +262,8 @@ const inviteController = async (req, res, next) => { .status(200) .json({ success: true, msg: "Invite sent", data: inviteToken }); } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined ? (error.method = "inviteController") : null; next(error); return; } @@ -283,7 +287,10 @@ const inviteVerifyController = async (req, res, next) => { .status(200) .json({ status: "success", msg: "Invite verified", data: invite }); } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined + ? (error.method = "inviteVerifyController") + : null; next(error); return; } @@ -306,7 +313,10 @@ const checkSuperadminController = async (req, res) => { data: superAdminExists, }); } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined + ? (error.method = "checkSuperadminController") + : null; next(error); return; } @@ -356,7 +366,10 @@ const recoveryRequestController = async (req, res, next) => { }); } } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined + ? (error.method = "recoveryRequestController") + : null; next(error); return; } @@ -391,7 +404,10 @@ const validateRecoveryTokenController = async (req, res, next) => { msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN, }); } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined + ? (error.method = "validateRecoveryTokenController") + : null; next(error); return; } @@ -427,7 +443,10 @@ const resetPasswordController = async (req, res, next) => { data: { user, token }, }); } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined + ? (error.method = "resetPasswordController") + : null; next(error); } }; @@ -482,8 +501,8 @@ const deleteUserController = async (req, res, next) => { msg: successMessages.AUTH_DELETE_USER, }); } catch (error) { - error.service = SERVICE_NAME; - error.method === undefined && error.method === "deleteUserController"; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined ? (error.method = "deleteUserController") : null; next(error); } }; @@ -495,7 +514,10 @@ const getAllUsersController = async (req, res) => { .status(200) .json({ success: true, msg: "Got all users", data: allUsers }); } catch (error) { - error.service = SERVICE_NAME; + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined + ? (error.method = "getAllUsersController") + : null; next(error); } };