Exception filter란?

Nest comes with a built-in exceptions layer which is responsible for processing all unhandled exceptions across an application. When an exception is not handled by your application code, it is caught by this layer, which then automatically sends an appropriate user-friendly response.

간단하게 설명하면, 프로그램 실행 중 예외가 발생하면, 해당 예외를 처리하는 코드로 라우팅을 해준다.

기본적인 exception 사용방법

Express에서 errorhandler를 구현하여 사용한 것처럼, 동일하게 사용한다.

@Get()
async findAll() {
  throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
}

단지 다른 점은 해당하는 Exception으로 던저 error를 만들어준다.

{
  "statusCode": 403,
  "message": "Forbidden"
}

The HttpException constructor takes two required arguments which determine the response:

내장되어 있는 HTTP exception 목록