Skip to content

Commit

Permalink
fix: initialize errorLogsService in ErrorHandlerMiddleware constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseAlbDR committed Mar 28, 2024
1 parent ff4f98f commit f1116a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/presentation/middlewares/error-handler.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,7 +42,7 @@ export class ErrorHandlerMiddleware {
message = err.message;
}

errorLogsService.addMessageToQueue(
this.errorLogsService.addMessageToQueue(
{
message: message,
level: statusCode === 500 ? 'high' : 'medium',
Expand Down
4 changes: 3 additions & 1 deletion src/presentation/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit f1116a4

Please sign in to comment.