<aside>

목차

</aside>

Spring의 GlobalExceptionHandler와 비교


<aside> ✏️

**NestJS**의 Exception Layer + Global Filter와 **Spring**의 @ControllerAdvice + @ExceptionHandler은 조합 및 개념적으로 거의 동일하다.

</aside>

NestJS vs Spring - 글로벌 예외 처리 비교

항목 NestJS Spring
예외 처리 계층 이름 Exception Layer + Global Exception Filter GlobalExceptionHandler (@ControllerAdvice)
필터 등록 방법 app.useGlobalFilters() @RestControllerAdvice 클래스 자동 등록
예외 클래스 계층 HttpException, BadRequestException, 등 Nest 제공 클래스 사용 ResponseStatusException,IllegalArgumentException, 커스텀 예외 등
커스텀 핸들러 작성 @Catch() + ExceptionFilter 구현 @ExceptionHandler 메서드
기본 처리기 BaseExceptionFilter ResponseEntityExceptionHandler
응답 형태 자동 변환 JSON으로 자동 변환 @ResponseBody 사용 시 JSON으로 자동 변환

코드 예제

<aside> 🗒️

정리

NestJSException LayerSpring의 @ControllerAdvice + @ExceptionHandler 구조와 유사한 책임과 역할을 한다.

</aside>

예제) NestJS의 Exception 처리


<aside> <img src="/icons/target_gray.svg" alt="/icons/target_gray.svg" width="40px" />

구성요소

코드

src/
├── common/
│   ├── exceptions/
│   │   ├── error-code.enum.ts
│   │   ├── business.exception.ts
│   │   └── global-exception.filter.ts