diff --git a/src/presentation/middlewares/error-handler.middleware.ts b/src/presentation/middlewares/error-handler.middleware.ts index 2d62af9..aa28ddc 100644 --- a/src/presentation/middlewares/error-handler.middleware.ts +++ b/src/presentation/middlewares/error-handler.middleware.ts @@ -9,18 +9,19 @@ import { QueueService } from '../common/services'; * Middleware class for handling errors. */ export class ErrorHandlerMiddleware { + errorLogsService: QueueService = new QueueService( + envs.RABBITMQ_URL, + 'error-notification' + ); + /** * Handles errors and sends appropriate responses. */ + constructor() {} - static handle(err: Error, _req: Request, res: Response, _next: NextFunction) { + handle(err: Error, _req: Request, res: Response, _next: NextFunction) { console.log({ err }); - const errorLogsService = new QueueService( - envs.RABBITMQ_URL, - 'error-notification' - ); - let message, statusCode; // Handle unknown errors @@ -41,7 +42,7 @@ export class ErrorHandlerMiddleware { message = err.message; } - errorLogsService.addMessageToQueue( + this.errorLogsService.addMessageToQueue( { message: message, level: statusCode === 500 ? 'high' : 'medium', diff --git a/src/presentation/server.ts b/src/presentation/server.ts index a7ef84d..7f10861 100644 --- a/src/presentation/server.ts +++ b/src/presentation/server.ts @@ -74,7 +74,9 @@ export class Server { //* Error Handler Middleware - this.app.use(ErrorHandlerMiddleware.handle); + const errorHandlerMiddleware = new ErrorHandlerMiddleware(); + + this.app.use(errorHandlerMiddleware.handle); //* Start the server and connect to the database this.serverListener = this.app.listen(this.port, async () => {