Skip to content

Commit a585656

Browse files
authored
[Fix/#79] 404 및 405 글로벌 예외 처리 추가 (#80)
## 📌 관련 이슈 - close #79 ## ✨ 변경 사항 - `NoResourceFoundException` 핸들링 추가 - `HttpRequestMethodNotSupportedException` 핸들링 추가 ## 📸 테스트 증명 (필수) <img width="325" height="98" alt="스크린샷 2026-03-27 오후 2 39 23" src="https://github.com/user-attachments/assets/6d5fec48-e37d-4493-9725-2ba36b443516" /> <img width="347" height="99" alt="스크린샷 2026-03-27 오후 2 39 31" src="https://github.com/user-attachments/assets/9f186fdd-3530-4107-a902-d0b7a30d582c" /> ## 📚 리뷰어 참고 사항 - ## ✅ 체크리스트 - [x] 브랜치 전략(git flow)을 따랐나요? - [x] 로컬에서 빌드 및 실행이 정상적으로 되나요? - [x] 불필요한 주석이나 더미 코드는 제거했나요? - [x] 컨벤션(커밋 메시지, 코드 스타일)을 지켰나요? - [x] spotlessApply 실행했나요? <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Improvements** * Enhanced error handling with clearer messages when requested resources cannot be found. * Improved error responses for unsupported HTTP request methods. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 187176a commit a585656

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/main/java/com/swyp/server/global/exception/ErrorCode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public enum ErrorCode {
1111
INVALID_INPUT_VALUE(40000, 400, "입력값이 올바르지 않습니다."),
1212
INTERNAL_SERVER_ERROR(50000, 500, "서버 오류가 발생했습니다."),
1313
DATA_INTEGRITY_VIOLATION(40900, 409, "요청을 처리할 수 없습니다."),
14+
RESOURCE_NOT_FOUND(40400, 404, "요청한 리소스를 찾을 수 없습니다."),
15+
METHOD_NOT_ALLOWED(40500, 405, "지원하지 않는 HTTP 메서드입니다."),
1416

1517
// Auth
1618
UNAUTHORIZED(40100, 401, "인증이 필요합니다."),

src/main/java/com/swyp/server/global/exception/GlobalExceptionHandler.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import jakarta.validation.ConstraintViolationException;
1010
import lombok.extern.slf4j.Slf4j;
1111
import org.springframework.dao.DataIntegrityViolationException;
12+
import org.springframework.http.HttpHeaders;
1213
import org.springframework.http.ResponseEntity;
1314
import org.springframework.http.converter.HttpMessageNotReadableException;
1415
import org.springframework.validation.FieldError;
16+
import org.springframework.web.HttpRequestMethodNotSupportedException;
1517
import org.springframework.web.bind.MethodArgumentNotValidException;
1618
import org.springframework.web.bind.MissingServletRequestParameterException;
1719
import org.springframework.web.bind.annotation.ExceptionHandler;
1820
import org.springframework.web.bind.annotation.RestControllerAdvice;
1921
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
22+
import org.springframework.web.servlet.resource.NoResourceFoundException;
2023

2124
@Slf4j
2225
@RestControllerAdvice
@@ -107,6 +110,34 @@ public ResponseEntity<ApiResponse<Void>> handleIntegrityViolationException(
107110
DATA_INTEGRITY_VIOLATION.getMessage()));
108111
}
109112

113+
@ExceptionHandler(NoResourceFoundException.class)
114+
public ResponseEntity<ApiResponse<Void>> handleNoResourceFoundException(
115+
NoResourceFoundException e) {
116+
log.warn("NoResourceFoundException: {}", e.getResourcePath());
117+
118+
return ResponseEntity.status(ErrorCode.RESOURCE_NOT_FOUND.getStatus())
119+
.body(
120+
ApiResponse.fail(
121+
ErrorCode.RESOURCE_NOT_FOUND.getCode(),
122+
ErrorCode.RESOURCE_NOT_FOUND.getMessage()));
123+
}
124+
125+
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
126+
public ResponseEntity<ApiResponse<Void>> handleMethodNotSupportedException(
127+
HttpRequestMethodNotSupportedException e) {
128+
log.warn("HttpRequestMethodNotSupportedException: {}", e.getMethod());
129+
130+
HttpHeaders headers = new HttpHeaders();
131+
headers.putAll(e.getHeaders());
132+
133+
return ResponseEntity.status(ErrorCode.METHOD_NOT_ALLOWED.getStatus())
134+
.headers(headers)
135+
.body(
136+
ApiResponse.fail(
137+
ErrorCode.METHOD_NOT_ALLOWED.getCode(),
138+
ErrorCode.METHOD_NOT_ALLOWED.getMessage()));
139+
}
140+
110141
@ExceptionHandler(Exception.class)
111142
public ResponseEntity<ApiResponse<Void>> handleException(Exception e) {
112143

0 commit comments

Comments
 (0)