diff --git a/src/shared/validateWithSchema.js b/src/shared/validateWithSchema.js index 2dd0b79..75128a8 100644 --- a/src/shared/validateWithSchema.js +++ b/src/shared/validateWithSchema.js @@ -10,6 +10,7 @@ import asyncErrorHandlerService from '../service/asyncErrorHandler.service.js'; import customValidationMessage from './customValidationMessage.js'; import httpStatus from '../constant/httpStatus.constants.js'; +import loggerService from '../service/logger.service.js'; /** * @function validateWithSchema @@ -36,14 +37,16 @@ const validateWithSchema = (schemas, options = {}) => { ); if (error) { + const message = error.details + .map((detail) => detail.message) + .join(', '); + loggerService.debug(message); const errorData = { route: req.originalUrl, timeStamp: new Date(), success: false, data: {}, - message: error.details - .map((detail) => detail.message) - .join(', '), + message, status: httpStatus.BAD_REQUEST, }; diff --git a/src/utilities/errorResponse.js b/src/utilities/errorResponse.js index 636c26e..3fe452a 100644 --- a/src/utilities/errorResponse.js +++ b/src/utilities/errorResponse.js @@ -8,6 +8,7 @@ */ import httpStatus from '../constant/httpStatus.constants.js'; +import loggerService from '../service/logger.service.js'; /** * errorResponse - A function that generates a standardized error response object. @@ -20,12 +21,17 @@ import httpStatus from '../constant/httpStatus.constants.js'; * @returns {Object} - An object representing the error response, including the timestamp, * success flag, data payload, error message, and status code. */ -const errorResponse = (message, status = httpStatus.BAD_REQUEST) => ({ - timeStamp: new Date(), - success: false, - data: {}, - message, - status, -}); +const errorResponse = (message, status = httpStatus.BAD_REQUEST) => { + toString(status).startsWith('5') + ? loggerService.error(message) + : loggerService.debug(message); + return { + timeStamp: new Date(), + success: false, + data: {}, + message, + status, + }; +}; export default errorResponse; diff --git a/src/utilities/sendResponse.js b/src/utilities/sendResponse.js index 55d3734..49d1c09 100644 --- a/src/utilities/sendResponse.js +++ b/src/utilities/sendResponse.js @@ -8,6 +8,7 @@ */ import httpStatus from '../constant/httpStatus.constants.js'; +import loggerService from '../service/logger.service.js'; /** * sendResponse - A function that creates a standardized response object for successful @@ -21,12 +22,17 @@ import httpStatus from '../constant/httpStatus.constants.js'; * @returns {Object} - An object representing the response, including the timestamp, * success flag, data payload, message, and status code. */ -const sendResponse = (data, message, status = httpStatus.OK) => ({ - timeStamp: new Date(), - success: true, - data, - message, - status, -}); +const sendResponse = (data, message, status = httpStatus.OK) => { + toString(status).startsWith('5') + ? loggerService.error(message) + : loggerService.debug(message); + return { + timeStamp: new Date(), + success: true, + data, + message, + status, + }; +}; export default sendResponse;