22
33import com .ktb .assignment .dto .ResponseDto ;
44import lombok .extern .slf4j .Slf4j ;
5+ import org .springframework .http .ResponseEntity ;
56import org .springframework .http .converter .HttpMessageNotReadableException ;
67import org .springframework .web .HttpRequestMethodNotSupportedException ;
7- import org .springframework .web .bind .MethodArgumentNotValidException ;
8- import org .springframework .web .bind .MissingServletRequestParameterException ;
98import org .springframework .web .bind .annotation .ExceptionHandler ;
109import org .springframework .web .bind .annotation .RestControllerAdvice ;
11- import org .springframework .web .method .annotation .MethodArgumentTypeMismatchException ;
1210import org .springframework .web .multipart .support .MissingServletRequestPartException ;
1311import org .springframework .web .servlet .NoHandlerFoundException ;
1412
1513@ Slf4j
1614@ RestControllerAdvice
1715public class GlobalExceptionHandler {
1816
19- // 지원되지 않는 HTTP 메소드를 사용할 때 발생하는 예외
20- @ ExceptionHandler (value = {NoHandlerFoundException .class , HttpRequestMethodNotSupportedException .class })
21- public ResponseDto <?> handleNoPageFoundException (Exception e ) {
22- log .error ("handleNoPageFoundException() in GlobalExceptionHandler throw NoHandlerFoundException : {}" ,
23- e .getMessage ());
24- return ResponseDto .fail (new CommonException (ErrorCode .NOT_FOUND_END_POINT ));
25- }
17+ // 존재하지 않는 API 요청 (404)
18+ @ ExceptionHandler ({NoHandlerFoundException .class , HttpRequestMethodNotSupportedException .class })
19+ public ResponseEntity <ResponseDto <?>> handleNoPageFoundException (Exception e ) {
20+ log .error ("handleNoPageFoundException: {}" , e .getMessage ());
2621
27- // @Validated 어노테이션을 사용하여 검증을 수행할 때 발생하는 예외
28- @ ExceptionHandler (value = {MethodArgumentNotValidException .class })
29- public ResponseDto <?> handleArgumentNotValidException (MethodArgumentNotValidException e ) {
30- log .error (
31- "handleArgumentNotValidException() in GlobalExceptionHandler throw MethodArgumentNotValidException : {}" ,
32- e .getMessage ());
33- return ResponseDto .fail (e );
22+ CommonException exception = new CommonException (ErrorCode .NOT_FOUND_END_POINT );
23+ return ResponseEntity
24+ .status (exception .getErrorCode ().getHttpStatus ()) // ✅ HTTP 상태 코드 직접 설정
25+ .body (ResponseDto .fail (exception ));
3426 }
3527
36- // 메소드의 인자 타입이 일치하지 않을 때 발생하는 예외
37- @ ExceptionHandler (value = {MethodArgumentTypeMismatchException .class })
38- public ResponseDto <?> handleArgumentNotValidException (MethodArgumentTypeMismatchException e ) {
39- log .error (
40- "handleArgumentNotValidException() in GlobalExceptionHandler throw MethodArgumentTypeMismatchException : {}" ,
41- e .getMessage ());
42- return ResponseDto .fail (e );
43- }
28+ // 필수 요청 파라미터가 누락된 경우 (400)
29+ @ ExceptionHandler (MissingServletRequestPartException .class )
30+ public ResponseEntity <ResponseDto <?>> handleServletRequestParameterException (MissingServletRequestPartException e ) {
31+ log .error ("handleServletRequestParameterException: {}" , e .getMessage ());
4432
45- // 필수 파라미터가 누락되었을 때 발생하는 예외
46- @ ExceptionHandler (value = {MissingServletRequestParameterException .class })
47- public ResponseDto <?> handleServletRequestParameterException (MissingServletRequestParameterException e ) {
48- log .error (
49- "handleArgumentNotValidException() in GlobalExceptionHandler throw MissingServletRequestParameterException : {}" ,
50- e .getMessage ());
51- return ResponseDto .fail (e );
33+ CommonException exception = new CommonException (ErrorCode .MISSING_REQUEST_PARAMETER );
34+ return ResponseEntity
35+ .status (exception .getErrorCode ().getHttpStatus ())
36+ .body (ResponseDto .fail (exception ));
5237 }
5338
54- // 필수 RequestPart가 누락되었을 때 발생하는 예외
55- @ ExceptionHandler (value = {MissingServletRequestPartException .class })
56- public ResponseDto <?> handleServletRequestParameterException (MissingServletRequestPartException e ) {
57- log .error (
58- "handleArgumentNotValidException() in GlobalExceptionHandler throw MissingServletRequestPartException : {}" ,
59- e .getMessage ());
60- return ResponseDto .fail (new CommonException (ErrorCode .MISSING_REQUEST_PARAMETER ));
61- }
39+ // 요청 Body가 없는 경우 (400)
40+ @ ExceptionHandler (HttpMessageNotReadableException .class )
41+ public ResponseEntity <ResponseDto <?>> handleMessageNotReadableException (HttpMessageNotReadableException e ) {
42+ log .error ("handleMessageNotReadableException: {}" , e .getMessage ());
6243
63- // post 요청 body가 없을 때 발생하는 에러
64- @ ExceptionHandler (value = {HttpMessageNotReadableException .class })
65- public ResponseDto <?> handleMessageNotReadableException (HttpMessageNotReadableException e ) {
66- log .error (
67- "handleArgumentNotValidException() in GlobalExceptionHandler throw HttpMessageNotReadableException : {}" ,
68- e .getMessage ());
69- return ResponseDto .fail (e );
44+ CommonException exception = new CommonException (ErrorCode .MISSING_REQUEST_BODY );
45+ return ResponseEntity
46+ .status (exception .getErrorCode ().getHttpStatus ())
47+ .body (ResponseDto .fail (exception ));
7048 }
7149
72- // 개발자가 직접 정의한 예외
73- @ ExceptionHandler (value = {CommonException .class })
74- public ResponseDto <?> handleApiException (CommonException e ) {
75- log .error ("handleApiException() in GlobalExceptionHandler throw CommonException : {}" , e .getMessage ());
76- return ResponseDto .fail (e );
50+ // 개발자가 직접 정의한 커스텀 예외
51+ @ ExceptionHandler (CommonException .class )
52+ public ResponseEntity <ResponseDto <?>> handleApiException (CommonException e ) {
53+ log .error ("handleApiException: {}" , e .getMessage ());
54+
55+ return ResponseEntity
56+ .status (e .getErrorCode ().getHttpStatus ())
57+ .body (ResponseDto .fail (e ));
7758 }
7859
79- // 서버, DB 예외
80- @ ExceptionHandler (value = {Exception .class })
81- public ResponseDto <?> handleException (Exception e ) {
82- log .error ("handleException() in GlobalExceptionHandle throw Exception : {}" );
83- e .printStackTrace ();
84- return ResponseDto .fail (new CommonException (ErrorCode .SERVER_ERROR ));
60+ // 서버 내부 오류 (500)
61+ @ ExceptionHandler (Exception .class )
62+ public ResponseEntity <ResponseDto <?>> handleException (Exception e ) {
63+ log .error ("handleException: {}" , e .getMessage (), e );
64+
65+ CommonException exception = new CommonException (ErrorCode .SERVER_ERROR );
66+ return ResponseEntity
67+ .status (exception .getErrorCode ().getHttpStatus ())
68+ .body (ResponseDto .fail (exception ));
8569 }
86- }
70+ }
0 commit comments