NestJS) Built-in HTTP exceptions

Songss·2025년 3월 3일

Nest.js

목록 보기
2/2

Nest는 HttpException을 상속받는 표준 예외(Exceptions) 집합을 제공합니다.

📌 NestJS 예외 클래스와 HTTP 상태 코드 목록

400 ~ 404까지 많이 사용되지 않을까 생각은 하지만 ! 전부 알아보자

예외 클래스HTTP 상태 코드설명
BadRequestException400 (Bad Request)잘못된 요청 (필수 필드 누락, 유효성 검사 실패)
UnauthorizedException401 (Unauthorized)인증되지 않은 사용자 (로그인 필요)
ForbiddenException403 (Forbidden)인증은 되었지만 권한 없음
NotFoundException404 (Not Found)리소스를 찾을 수 없음
MethodNotAllowedException405 (Method Not Allowed)허용되지 않은 HTTP 메서드 사용
NotAcceptableException406 (Not Acceptable)요청이 서버에서 지원되지 않는 형식
RequestTimeoutException408 (Request Timeout)요청 시간이 초과됨
ConflictException409 (Conflict)리소스 충돌 (중복 데이터 등)
GoneException410 (Gone)리소스가 삭제됨 (더 이상 유효하지 않음)
PayloadTooLargeException413 (Payload Too Large)요청 데이터가 너무 큼
UnsupportedMediaTypeException415 (Unsupported Media Type)지원되지 않는 요청 타입 (예: application/xml 요청했는데 JSON만 지원)
UnprocessableEntityException422 (Unprocessable Entity)요청 데이터는 정상적이지만 처리할 수 없음
InternalServerErrorException500 (Internal Server Error)서버 내부 오류
NotImplementedException501 (Not Implemented)구현되지 않은 기능 호출
BadGatewayException502 (Bad Gateway)게이트웨이 또는 프록시 서버 오류
ServiceUnavailableException503 (Service Unavailable)서버가 일시적으로 사용 불가
GatewayTimeoutException504 (Gateway Timeout)게이트웨이 요청 시간 초과
PreconditionFailedException412 (Precondition Failed)요청의 사전 조건이 충족되지 않음
HttpVersionNotSupportedException505 (HTTP Version Not Supported)지원되지 않는 HTTP 버전
ImATeapotException418 (I'm a teapot)장난식 예외 (RFC 2324: "I'm a teapot")

🎯 예외 처리 예시

import { BadRequestException, NotFoundException, InternalServerErrorException } from '@nestjs/common';

throw new BadRequestException('잘못된 요청입니다. 필수 데이터를 확인하세요.');
throw new NotFoundException('해당 데이터를 찾을 수 없습니다.');
throw new InternalServerErrorException('서버 내부 오류가 발생했습니다.');

---


0개의 댓글