@@ -4,8 +4,15 @@ import com.nomoney.exception.ClientException
44import com.nomoney.exception.NoMoneyException
55import org.springframework.http.HttpStatus
66import org.springframework.http.ResponseEntity
7+ import org.springframework.http.converter.HttpMessageNotReadableException
8+ import org.springframework.web.HttpMediaTypeNotSupportedException
9+ import org.springframework.web.HttpRequestMethodNotSupportedException
10+ import org.springframework.web.bind.MethodArgumentNotValidException
11+ import org.springframework.web.bind.MissingServletRequestParameterException
712import org.springframework.web.bind.annotation.ExceptionHandler
813import org.springframework.web.bind.annotation.RestControllerAdvice
14+ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
15+ import org.springframework.web.servlet.NoHandlerFoundException
916
1017@RestControllerAdvice
1118class GlobalExceptionHandler {
@@ -30,6 +37,77 @@ class GlobalExceptionHandler {
3037 return ResponseEntity .status(HttpStatus .INTERNAL_SERVER_ERROR ).body(errorResponse)
3138 }
3239
40+ @ExceptionHandler(MethodArgumentNotValidException ::class )
41+ fun handleMethodArgumentNotValidException (ex : MethodArgumentNotValidException ): ResponseEntity <ErrorResponse > {
42+ val errors = ex.bindingResult.fieldErrors.joinToString(" , " ) { " ${it.field} : ${it.defaultMessage} " }
43+ val errorResponse = ErrorResponse (
44+ code = " E400" ,
45+ message = " 잘못된 요청입니다." ,
46+ messageForDev = " 검증 실패: $errors " ,
47+ )
48+ return ResponseEntity .status(HttpStatus .BAD_REQUEST ).body(errorResponse)
49+ }
50+
51+ @ExceptionHandler(HttpMessageNotReadableException ::class )
52+ fun handleHttpMessageNotReadableException (ex : HttpMessageNotReadableException ): ResponseEntity <ErrorResponse > {
53+ val errorResponse = ErrorResponse (
54+ code = " E400" ,
55+ message = " 잘못된 요청입니다." ,
56+ messageForDev = " 요청 본문을 읽을 수 없습니다: ${ex.message} " ,
57+ )
58+ return ResponseEntity .status(HttpStatus .BAD_REQUEST ).body(errorResponse)
59+ }
60+
61+ @ExceptionHandler(MissingServletRequestParameterException ::class )
62+ fun handleMissingServletRequestParameterException (ex : MissingServletRequestParameterException ): ResponseEntity <ErrorResponse > {
63+ val errorResponse = ErrorResponse (
64+ code = " E400" ,
65+ message = " 잘못된 요청입니다." ,
66+ messageForDev = " 필수 파라미터 누락: ${ex.parameterName} (타입: ${ex.parameterType} )" ,
67+ )
68+ return ResponseEntity .status(HttpStatus .BAD_REQUEST ).body(errorResponse)
69+ }
70+
71+ @ExceptionHandler(MethodArgumentTypeMismatchException ::class )
72+ fun handleMethodArgumentTypeMismatchException (ex : MethodArgumentTypeMismatchException ): ResponseEntity <ErrorResponse > {
73+ val errorResponse = ErrorResponse (
74+ code = " E400" ,
75+ message = " 잘못된 요청입니다." ,
76+ messageForDev = " 파라미터 타입 불일치 '${ex.name} ': 기대값 ${ex.requiredType?.simpleName} , 입력값 ${ex.value} " ,
77+ )
78+ return ResponseEntity .status(HttpStatus .BAD_REQUEST ).body(errorResponse)
79+ }
80+
81+ @ExceptionHandler(HttpRequestMethodNotSupportedException ::class )
82+ fun handleHttpRequestMethodNotSupportedException (ex : HttpRequestMethodNotSupportedException ): ResponseEntity <ErrorResponse > {
83+ val errorResponse = ErrorResponse (
84+ code = " E405" ,
85+ message = " 지원하지 않는 HTTP 메서드입니다." ,
86+ messageForDev = " 메서드 '${ex.method} '는 지원되지 않습니다. 지원되는 메서드: ${ex.supportedHttpMethods?.joinToString(" , " )} " ,
87+ )
88+ return ResponseEntity .status(HttpStatus .METHOD_NOT_ALLOWED ).body(errorResponse)
89+ }
90+
91+ @ExceptionHandler(HttpMediaTypeNotSupportedException ::class )
92+ fun handleHttpMediaTypeNotSupportedException (ex : HttpMediaTypeNotSupportedException ): ResponseEntity <ErrorResponse > {
93+ val errorResponse = ErrorResponse (
94+ code = " E415" ,
95+ message = " 지원하지 않는 미디어 타입입니다." ,
96+ messageForDev = " Content type '${ex.contentType} '는 지원되지 않습니다. 지원되는 타입: ${ex.supportedMediaTypes.joinToString(" , " )} " ,
97+ )
98+ return ResponseEntity .status(HttpStatus .UNSUPPORTED_MEDIA_TYPE ).body(errorResponse)
99+ }
100+
101+ @ExceptionHandler(NoHandlerFoundException ::class )
102+ fun handleNoHandlerFoundException (ex : NoHandlerFoundException ): ResponseEntity <ErrorResponse > {
103+ val errorResponse = ErrorResponse (
104+ code = " E404" ,
105+ message = " 요청한 리소스를 찾을 수 없습니다." ,
106+ messageForDev = " 핸들러를 찾을 수 없습니다: ${ex.httpMethod} ${ex.requestURL} " ,
107+ )
108+ return ResponseEntity .status(HttpStatus .NOT_FOUND ).body(errorResponse)
109+ }
110+
33111 @ExceptionHandler(Exception ::class )
34112 fun handleException (ex : Exception ): ResponseEntity <ErrorResponse > {
35113 val errorResponse = ErrorResponse (
0 commit comments