From d8ce38513400db8da60c504641166bd9e62ebf5a Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 7 Mar 2025 22:27:13 +0100 Subject: [PATCH] refactor(routes): refactor handleException and get rid of "any" type --- src/routes/routes.ts | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/routes/routes.ts b/src/routes/routes.ts index 7d5fea44b..ad1850aca 100644 --- a/src/routes/routes.ts +++ b/src/routes/routes.ts @@ -493,22 +493,20 @@ function handleResponse(resultHandler: ApiResultHandler, req: express.Request, r log.request(req, res, Date.now() - start, responseLength); } -function handleException(e: any, method: HttpMethod, path: string, res: express.Response) { - log.error(`${method} ${path} threw exception: '${e.message}', stack: ${e.stack}`); +function handleException(e: unknown | Error, method: HttpMethod, path: string, res: express.Response) { + + const [errMessage, errStack]: [message: string, stack: string] = (e instanceof Error) + ? [e.message, e.stack || "No Stack Trace"] + : ["Unknown Error", "No Stack Trace"]; + + log.error(`${method} ${path} threw exception: '${errMessage}', stack: ${errStack}`); + + const resStatusCode = (e instanceof ValidationError || e instanceof NotFoundError) ? e.statusCode : 500; + + res.status(resStatusCode).json({ + message: errMessage + }); - if (e instanceof ValidationError) { - res.status(400).json({ - message: e.message - }); - } else if (e instanceof NotFoundError) { - res.status(404).json({ - message: e.message - }); - } else { - res.status(500).json({ - message: e.message - }); - } } function createUploadMiddleware() {