From d00fd913000e8d01e5a73fb127fcd4d3a20a688e Mon Sep 17 00:00:00 2001 From: NIYOMUGABO BERNARD <85235653+niyobern@users.noreply.github.com> Date: Thu, 2 May 2024 15:51:43 +0000 Subject: [PATCH 1/2] Removed unnecesary type annotation and console logs in errorHandler --- src/middlewares/errorHandler.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/middlewares/errorHandler.ts b/src/middlewares/errorHandler.ts index 57800567..ad711400 100644 --- a/src/middlewares/errorHandler.ts +++ b/src/middlewares/errorHandler.ts @@ -5,8 +5,7 @@ function errorHandler(func: Function) { return async (req: Request, res: Response, next: NextFunction) => { try { await func(req, res, next); - } catch (error: any) { - console.log({ error }); + } catch (error) { // Removed the type annotation from the catch clause variable because it caused liting errors const message = error.detail || 'Internal Server Error'; res.status(500).send(message); } From 0660a853daa796fa758e8eefecd06385f0f07eba Mon Sep 17 00:00:00 2001 From: NIYOMUGABO BERNARD <85235653+niyobern@users.noreply.github.com> Date: Thu, 2 May 2024 15:55:52 +0000 Subject: [PATCH 2/2] Refactor errorHandler to use MiddlewareFunction in errorHandler.ts --- src/middlewares/errorHandler.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/middlewares/errorHandler.ts b/src/middlewares/errorHandler.ts index ad711400..9837635f 100644 --- a/src/middlewares/errorHandler.ts +++ b/src/middlewares/errorHandler.ts @@ -1,7 +1,8 @@ import { Request, Response, NextFunction } from 'express'; +type MiddlewareFunction = (req: Request, res: Response, next: NextFunction) => Promise; -function errorHandler(func: Function) { +function errorHandler(func: MiddlewareFunction) { return async (req: Request, res: Response, next: NextFunction) => { try { await func(req, res, next);