From 648f9f7b1a1f64fe016a37a6b5c2e1778a4b2114 Mon Sep 17 00:00:00 2001 From: Glemen Neo Date: Tue, 17 Sep 2024 02:30:12 +0800 Subject: [PATCH] [#7] feat: add more logging for app routes --- backend/user-service/src/index.ts | 28 +++++++------------ .../user-service/src/types/ResponseError.ts | 3 -- 2 files changed, 10 insertions(+), 21 deletions(-) delete mode 100644 backend/user-service/src/types/ResponseError.ts diff --git a/backend/user-service/src/index.ts b/backend/user-service/src/index.ts index 38be5ce37d..e3be2f5f2b 100644 --- a/backend/user-service/src/index.ts +++ b/backend/user-service/src/index.ts @@ -1,7 +1,7 @@ import cors from 'cors' import express, { Express, NextFunction, Request, Response } from 'express' import helmet from 'helmet' -import { ResponseError } from './types/ResponseError' +import { logger } from './server' const app: Express = express() @@ -27,28 +27,20 @@ app.use((request: Request, response: Response, next: NextFunction) => { next() }) +// Health Check Route app.get('/', (_: Request, response: Response) => { - response - .json({ - message: 'Hello World!', - }) - .status(200) + response.status(200) }) -// Handle When No Route Match Is Found -app.use((next: NextFunction) => { - const error: ResponseError = new Error() - error.status = 404 - next(error) +// Not Found Route +app.use((response: Response) => { + response.status(404) }) -app.use((error: ResponseError, _: Request, response: Response) => { - response.status(error.status ?? 500) - response.json({ - error: { - message: error.message, - }, - }) +// Default Error Handler +app.use((error: Error, request: Request, response: Response) => { + logger.error(`[Controller] [${request.method} ${request.baseUrl + request.path}] ${error.message}`) + response.status(500) }) export default app diff --git a/backend/user-service/src/types/ResponseError.ts b/backend/user-service/src/types/ResponseError.ts deleted file mode 100644 index ac348a97ac..0000000000 --- a/backend/user-service/src/types/ResponseError.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface ResponseError extends Error { - status?: number -}