Skip to content

Commit 200a933

Browse files
authored
Merge pull request #2621 from bluewave-labs/fix/consistent-error-handling
fix: imporved and consistent error handling
2 parents bfba1d1 + 38bb915 commit 200a933

15 files changed

+684
-853
lines changed

server/controllers/announcementsController.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createAnnouncementValidation } from "../validation/joi.js";
2-
import { handleError } from "./controllerUtils.js";
2+
import { asyncHandler } from "../utils/errorUtils.js";
33

44
const SERVICE_NAME = "announcementController";
55

@@ -28,16 +28,10 @@ class AnnouncementController {
2828
*
2929
* @returns {Promise<void>} A promise that resolves once the response is sent.
3030
*/
31-
createAnnouncement = async (req, res, next) => {
32-
try {
31+
createAnnouncement = asyncHandler(
32+
async (req, res, next) => {
3333
await createAnnouncementValidation.validateAsync(req.body);
34-
} catch (error) {
35-
return next(handleError(error, SERVICE_NAME)); // Handle Joi validation errors
36-
}
37-
38-
const { title, message } = req.body;
39-
40-
try {
34+
const { title, message } = req.body;
4135
const announcementData = {
4236
title: title.trim(),
4337
message: message.trim(),
@@ -49,10 +43,10 @@ class AnnouncementController {
4943
msg: this.stringService.createAnnouncement,
5044
data: newAnnouncement,
5145
});
52-
} catch (error) {
53-
next(handleError(error, SERVICE_NAME, "createAnnouncement"));
54-
}
55-
};
46+
},
47+
SERVICE_NAME,
48+
"createAnnouncement"
49+
);
5650

5751
/**
5852
* Handles retrieving announcements with pagination.
@@ -63,17 +57,17 @@ class AnnouncementController {
6357
* - `msg`: A message about the success of the request.
6458
* @param {Function} next - The next middleware function in the stack for error handling.
6559
*/
66-
getAnnouncement = async (req, res, next) => {
67-
try {
60+
getAnnouncement = asyncHandler(
61+
async (req, res, next) => {
6862
const allAnnouncements = await this.db.getAnnouncements();
6963
return res.success({
7064
msg: this.stringService.getAnnouncement,
7165
data: allAnnouncements,
7266
});
73-
} catch (error) {
74-
next(handleError(error, SERVICE_NAME, "getAnnouncement"));
75-
}
76-
};
67+
},
68+
SERVICE_NAME,
69+
"getAnnouncement"
70+
);
7771
}
7872

7973
export default AnnouncementController;

0 commit comments

Comments
 (0)