From 6c4325af73fe9fbd59c0c615a2b8d0748204148d Mon Sep 17 00:00:00 2001 From: anniemon Date: Mon, 23 Dec 2024 01:45:17 +0900 Subject: [PATCH 1/2] =?UTF-8?q?application:=20=EA=B8=80=EB=A1=9C=EB=B2=8C?= =?UTF-8?q?=20exception=20handler,=20validation=20pipe=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filters/global-exception.filter.ts | 40 +++++++++++++++++++ src/application/modules/app.module.ts | 5 +++ src/main.ts | 8 ++++ 3 files changed, 53 insertions(+) create mode 100644 src/application/filters/global-exception.filter.ts diff --git a/src/application/filters/global-exception.filter.ts b/src/application/filters/global-exception.filter.ts new file mode 100644 index 0000000..801f7d1 --- /dev/null +++ b/src/application/filters/global-exception.filter.ts @@ -0,0 +1,40 @@ +import { + Catch, + ArgumentsHost, + HttpStatus, + HttpException, + Logger, +} from '@nestjs/common'; +import { Response } from 'express'; + +@Catch() +export class HttpExceptionsFilter { + private readonly logger: Logger = new Logger('ExceptionsFilter'); + + public catch(exception: unknown, host: ArgumentsHost): void { + const ctx = host.switchToHttp(); + const res = ctx.getResponse(); + + let status: number = HttpStatus.INTERNAL_SERVER_ERROR; + let response: string | object = { + statusCode: status, + message: 'Internal server error', + }; + if (exception instanceof HttpException) { + status = exception.getStatus(); + response = exception.getResponse(); + } + + if (status >= HttpStatus.INTERNAL_SERVER_ERROR) { + this.logger.error( + `Http status: ${status} Error Response: ${JSON.stringify(response)}`, + ); + } else { + this.logger.warn( + `Http status: ${status} Error Response: ${JSON.stringify(response)}`, + ); + } + + res.status(status).json(response); + } +} diff --git a/src/application/modules/app.module.ts b/src/application/modules/app.module.ts index bd75df4..c8bb0aa 100644 --- a/src/application/modules/app.module.ts +++ b/src/application/modules/app.module.ts @@ -4,6 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import dataSource from '@infrastructure/typeorm/data-source'; import { AppController } from '@application/controllers'; import { AppService } from '@domain/services'; +import { HttpExceptionsFilter } from '../filters/global-exception.filter'; const modulesList = Object.keys(modules).map( (moduleIndex) => modules[moduleIndex as keyof typeof modules], @@ -20,6 +21,10 @@ const modulesList = Object.keys(modules).map( provide: 'DataSource', useValue: dataSource, }, + { + provide: 'APP_FILTER', + useClass: HttpExceptionsFilter, + }, ], controllers: [AppController], }) diff --git a/src/main.ts b/src/main.ts index 5fdcdb6..b7a633a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,18 @@ import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { AppModule } from '@application/modules/app.module'; +import { ValidationPipe } from '@nestjs/common'; async function bootstrap() { const app = await NestFactory.create(AppModule); + app.useGlobalPipes( + new ValidationPipe({ + transform: true, + transformOptions: { enableImplicitConversion: true }, + }), + ); + const swaggerConfig = new DocumentBuilder() .setTitle('E-commerce API') .setDescription('E-commerce API 문서') From 5417721983a2f8a3fc14086f57b39e157bcbc598 Mon Sep 17 00:00:00 2001 From: anniemon Date: Mon, 23 Dec 2024 01:46:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?application:=20=ED=8F=AC=EC=9D=B8=ED=8A=B8?= =?UTF-8?q?=20=EC=B6=A9=EC=A0=84=20=EA=B8=88=EC=95=A1=20=EC=9D=8C=EC=88=98?= =?UTF-8?q?=EC=9D=BC=20=EB=96=84=20=EC=98=88=EC=99=B8=20=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/application/dtos/point.dto.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/application/dtos/point.dto.ts b/src/application/dtos/point.dto.ts index d17c506..8b96b1d 100644 --- a/src/application/dtos/point.dto.ts +++ b/src/application/dtos/point.dto.ts @@ -6,7 +6,7 @@ export class PointDto { default: 1000, }) @IsInt() - @IsPositive() + @IsPositive({ message: '금액은 0보다 커야 합니다.' }) amount: number; @ApiProperty({