- 파일 만들기 => commons/filter => http-exception.filter.ts
import {
Catch,
ExceptionFilter,
HttpException,
InternalServerErrorException,
} from '@nestjs/common';
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException) {
const status = exception.getStatus();
const message = exception.message;
console.log('예외 발생', message, status);
throw new InternalServerErrorException('서버 오류');
}
}
- main.ts 파일 수정
app.useGlobalFilters(new HttpExceptionFilter());
근데 GlobalFilters를 설정안해도 기본적으로 500코드를 반환한다고 하니 위에 위에 코드를 굳이 작성할 필요는 없을듯.
아마 따로 예외코드들과 그에 따른 응답을 지정해줘야할때 필요할듯.
++ 에러 코드를 변경할 때 사용하며 좋음.
https://springdream0406.tistory.com/192
Exception Filter
- Exception Filter란?NestJS에서는 자체적으로 예외 레이어를 관리한다.서버에서 발생한 예외가 따로 핸들링 되지 않으면 NestJS 예외 레이어에서 에어를 사용자 친화적으로 변환해서 응답할 수 있다.
springdream0406.tistory.com
https://docs.nestjs.com/exception-filters#binding-filters
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Rea
docs.nestjs.com