add error handling util, update middleware and controllers

This commit is contained in:
Alex Holliday committed 2025-07-15 16:33:35 -07:00
1 parent c54feb9306
commit 38bb915144
15 files changed
+683 -852

No files matched your search

+14 -20
View File
@@ -1,5 +1,5 @@
import { createAnnouncementValidation } from "../validation/joi.js";
import { handleError } from "./controllerUtils.js";
import { asyncHandler } from "../utils/errorUtils.js";
const SERVICE_NAME = "announcementController";
@@ -28,16 +28,10 @@ class AnnouncementController {
*
* @returns {Promise<void>} A promise that resolves once the response is sent.
*/
createAnnouncement = async (req, res, next) => {
try {
createAnnouncement = asyncHandler(
async (req, res, next) => {
await createAnnouncementValidation.validateAsync(req.body);
} catch (error) {
return next(handleError(error, SERVICE_NAME)); // Handle Joi validation errors
}
const { title, message } = req.body;
try {
const { title, message } = req.body;
const announcementData = {
title: title.trim(),
message: message.trim(),
@@ -49,10 +43,10 @@ class AnnouncementController {
msg: this.stringService.createAnnouncement,
data: newAnnouncement,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "createAnnouncement"));
}
};
},
SERVICE_NAME,
"createAnnouncement"
);
/**
* Handles retrieving announcements with pagination.
@@ -63,17 +57,17 @@ class AnnouncementController {
* - `msg`: A message about the success of the request.
* @param {Function} next - The next middleware function in the stack for error handling.
*/
getAnnouncement = async (req, res, next) => {
try {
getAnnouncement = asyncHandler(
async (req, res, next) => {
const allAnnouncements = await this.db.getAnnouncements();
return res.success({
msg: this.stringService.getAnnouncement,
data: allAnnouncements,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getAnnouncement"));
}
};
},
SERVICE_NAME,
"getAnnouncement"
);
}
export default AnnouncementController;