NestJS 에러 핸들링 핵심 개념 The core concepts of error handling in NestJS

이은지·2023년 6월 20일
0
post-thumbnail

총 세 가지 핵심 개념이 존재한다.
1. exception interceptor
2. exception filter
3. exception layer

exception interceptor

exception filter, exception layer에 도달하기 전에 exception을 인터셉트해 exception을 가공한다.

어떻게 가공하는가?

  • exception에 대한 추가적인 정보를 덧붙인다.

    • e.g. 유저 정보, 요청 헤더, 등
  • 라우트/컨트롤러와 무관한 에러 핸들링 작업을 수행한다.

    • 규격/추상화 레벨을 맞추는 작업

      • error formatting
      • error wrapping: 에러를 추상화
    • 로깅

exception filter

exception을 처리한다.

  • exception을 적절한 HTTP 응답으로 변환한다.
  • exception 유형에 따라 적절한 조치를 취한다.
  • 라우트/컨트롤러별 exception filter와, 모든 라우트/컨트롤러에 적용되는 global exception filter를 정의할 수 있다.

exception layer

NextJS에서 exception의 최종 종착지(final fallback mechanism)

  • exception filter에 의해 처리되었는지와 관계없이, 모든 exception은 exception layer에 도착한다.
  • 처리되지 않은 exception을 처리한다. 처리되지 않은 exception이 앱을 크래쉬하지 않도록, exception을 클라이언트에게 전달될 수 있는 형태의 HTTP 에러 응답으로 변환한다.
profile
교육학과 출신 서타터업 프론트 개발자 👩🏻‍🏫

0개의 댓글