1. Exception Filter ์๊ฐ

- Exception Filter์์ ์ํํ ๋ถ๋ถ๋ค์ interceptor์์ ์ถ๊ฐ ๋ก์ง์ ๋ ๊ตฌํํ ์ ์๋ค.
- Exception Filter๋ ๋ง ๊ทธ๋๋ก ์์ธ๋ฅผ ํํฐ๋ง ํ๋ ๊ธฐ๋ฅ์ด๋ค.
- NestJS์์๋ ์์ฒด์ ์ผ๋ก ์์ธ ๋ ์ด์ด๋ฅผ ๊ด๋ฆฌํ๋ค.
- ์๋ฒ์์ ๋ฐ์ํ ์์ธ๊ฐ ๋ฐ๋ก ํธ๋ค๋ง ๋์ง ์์ผ๋ฉด NestJS ์์ธ ๋ ์ด์ด์์ ์๋ฌ๋ฅผ ์ฌ์ฉ์ ์นํ์ ์ผ๋ก ๋ณํํด์ ์๋ต ํ ์ ์๋ค
- ์๋ต์ ๋ณํํ๋ ๋ถ๋ถ์ ์ฌ์ค ํฌ๊ฒ ์ธ ์ผ์ด ๋ง์ง๋ ์๋ค.
- ์๋๋ฉด์ ์์ธ๋ผ๋ ๊ฒ ์์ฒด๊ฐ ์์ธ๊ฐ ๋์ ธ์ก์ ๋,
- ๋์ ธ์ง ์๋ ๊ทธ๋๋ก ์ฌ์ฉ๋์ด์ผ ํ๋ก ํธ์๋์์๋ ์๋ฏธ๊ฐ ์๋ ๊ฒฝ์ฐ๊ฐ ๋๊ฒ ๋ง๋ค.
- ๊ทธ๋์ ์์ธ ์ฒ๋ฆฌ๋ฅผ ๋ง์ด ๋ณํํ๋ ๊ฒฝ์ฐ๋ ํฌ๊ฒ ์๋ค.
- Exception Filter๊ฐ ๋๊ฒ ์ ์ฉํ ๊ฒฝ์ฐ
- ์๋ฌ๊ฐ ์๊ฒผ์ ๋ TGS ์๋ฒ ๋ด๋ถ์์ ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๊ฑฐ๋
- ์๋๋ฉด ๋ชจ๋ํฐ๋ง ํ ์ ์๋ ์์คํ ํํ ์๋ ค์ฃผ๋ ๋ก์ง์ ์์ฑํ ๋

- ๊ณต์๋ฌธ์ : https://docs.nestjs.com/exception-filters
- ๊ณต์๋ฌธ์์ ๊ทธ๋ฆผ์ ๋ณด๋ฉด, ํด๋ผ์ด์ธํธ ์ฌ์ด๋์์ ์์ฒญ์ ๋ณด๋ด๊ณ ,
- ์๋ต์ ๋ฐ์ ๋, Filter๊ฐ ์ ์ฉ๋๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
- https://docs.nestjs.com/exception-filters#built-in-http-exceptions
- ์ด Exceptions๋ค์ ๋ชจ๋ HTTP Exceptions์ Extend๋ฅผ ํ๊ณ ์๋ค.
- ๊ทธ๋์ ๋์ค์ Exception ํํฐ ํด๋์ค๋ฅผ ๋ง๋ค ๋,
- HTTP Exceptions์ ํด๋น๋๋ Exceptions๋ค๋ง ์ก๋ Exception ํํฐ๋ฅผ ๋ง๋ญ๋๋ค.
1.1 Exception Filter ๊ตฌํ๋ฐฉ๋ฒ
1import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common'2import { Request, Response } from 'express'34@Catch(HttpException)5export class HttpExceptionFilter implements ExceptionFilter {6catch(exception: HttpException, host: ArgumentsHost) {7const ctx = host.switchToHttp()8const response = ctx.getResponse<Response>()9const request = ctx.getRequest<Request>()10const status = exception.getStatus()1112response.status(status).json({13statusCode: status,14message: exception.message,15timestamp: new Date().toLocaleString('kr'),16})17}18}
2. HttpExceptionFilter ๋ง๋ค๊ธฐ
common/exception-filter/http.exception-filter.ts ํ์ผ์ ๋ง๋ ๋ค.
common/exception-filter/http.exception-filter.ts
1import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/common'23@Catch(HttpException)4export class HttpExceptionFilter implements ExceptionFilter {5catch(exception: HttpException, host: ArgumentsHost) {6const ctx = host.switchToHttp()7const response = ctx.getResponse()8const request = ctx.getRequest()9const status = exception.getStatus()1011/**** Filter ์ ์ญ ์ค์ 12* ๋ก๊ทธ ํ์ผ์ ์์ฑํ๊ฑฐ๋,13* ์๋ฌ ๋ชจ๋ํฐ๋ง ์์คํ ์ API ์์ฒญํ๊ธฐ ๊ธฐ๋ฅ์ ๋ง๋ค๊ณ14* globalํ๊ฒ ์ธ ์๋ ์์(main.ts)15*/1617response.status(status).json({18statusCode: status,19message: exception.message,20timestamp: new Date().toLocaleString('kr'),21path: request.url,22})23}24}
์ด์ ์์ฑํ Filter๋ฅผ posts ์ปจํ๋ก๋ฌ์ getPosts์ ์ ์ฉํด๋ณธ๋ค.
posts.controller.ts
1// posts.controller.ts ์๋ต2/*** 1) GET /posts3* ๋ชจ๋ post๋ฅผ ๋ค ๊ฐ์ ธ์จ๋ค4*/5@Get()6@UseInterceptors(LogInterceptor)7@UseFilters(HttpExceptionFilter)8getPosts(@Query() query: PaginatePostDto) {9throw new BadRequestException('์๋ฌ ํ ์คํธ')10return this.postsService.paginatePosts(query)11}
์ด์ ์ผ๋ถ๋ฌ ์๋ฌ๋ฅผ ๋ด๋ฉด, ์ฐ๋ฆฌ๊ฐ ์ ์ํ๋๋ก ์๋ต์ด ์จ ๊ฒ์ ํ์ธํ ์ ์๋ค.
1{2"statusCode": 400,3"message": "์๋ฌ ํ ์คํธ",4"timestamp": "2024. 1. 7. ์คํ 5:44:51",5"path": "/posts?order__createdAt=DESC&take=5"6}
์ฐธ๊ณ ๋ก main.ts์์ Filter๋ฅผ ๊ธ๋ก๋ฒํ๊ฒ ์ ์ฉํ ์ ์๋ค.
- ์ด๋ ๊ฒ ํ๋ฉด ์ฑ ์ ์ฒด์์ nhttp exception ๊ด๋ จ๋ ๋ชจ๋ exception์ด ๋ฐ์์ ํ ๋๋ง๋ค,
- ๋ก๊ทธ ๋ชจ๋ํฐ๋ง ํ ์ ์๋ ๋ก์ง์ ์์ฑ์ ํ๋ค๊ณ ๊ฐ์ ์ ํ์ ๋,
- ์๋ฌ๊ฐ ๋ ๋๋ง๋ค ๋ชจ๋ ์๋ฌ๋ค์ ๋ํด์ ์ ๋ถ http exception ํํฐ๋ฅผ ์คํ์ ํด์ค ์๊ฐ ์๋ค.
- ์ง๊ธ์ ๊ธ๋ก๋ฒํ๊ฒ ๊ทธ๋ฅ ์ธ ํ์๊ฐ ์์ผ๋๊น, ์ผ๋จ์ ์ฃผ์ ์ฒ๋ฆฌํ๋ค.
main.ts
1// ์๋ต2async function bootstrap() {3// ์๋ต4app.useGlobalFilters(new HttpExceptionFilter()) // ์ฃผ์ ์ฒ๋ฆฌ5await app.listen(3000)6}7bootstrap()