Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/application/dtos/point.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class PointDto {
default: 1000,
})
@IsInt()
@IsPositive()
@IsPositive({ message: '금액은 0보다 커야 합니다.' })
amount: number;

@ApiProperty({
Expand Down
40 changes: 40 additions & 0 deletions src/application/filters/global-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -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<Response>();

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);
}
}
5 changes: 5 additions & 0 deletions src/application/modules/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -20,6 +21,10 @@ const modulesList = Object.keys(modules).map(
provide: 'DataSource',
useValue: dataSource,
},
{
provide: 'APP_FILTER',
useClass: HttpExceptionsFilter,
},
],
controllers: [AppController],
})
Expand Down
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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 문서')
Expand Down
Loading