@@ -15,10 +15,12 @@ import org.springframework.http.converter.HttpMessageNotReadableException
1515import org.springframework.security.authentication.AuthenticationServiceException
1616import org.springframework.security.authentication.BadCredentialsException
1717import org.springframework.security.authentication.InsufficientAuthenticationException
18+ import org.springframework.web.bind.MethodArgumentNotValidException
1819import org.springframework.web.bind.annotation.ExceptionHandler
1920import org.springframework.web.bind.annotation.RestControllerAdvice
2021import org.springframework.web.context.request.WebRequest
2122import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
23+ import org.springframework.web.util.BindErrorUtils
2224
2325@Order(Ordered .HIGHEST_PRECEDENCE )
2426@RestControllerAdvice
@@ -42,6 +44,26 @@ open class CsmExceptionHandling : ResponseEntityExceptionHandler() {
4244 return super .handleExceptionInternal(exception, problemDetail, headers, status, request)
4345 }
4446
47+ override fun handleMethodArgumentNotValid (
48+ exception : MethodArgumentNotValidException ,
49+ headers : HttpHeaders ,
50+ status : HttpStatusCode ,
51+ request : WebRequest
52+ ): ResponseEntity <Any >? {
53+ val badRequestStatus = HttpStatus .BAD_REQUEST
54+ val problemDetail = ProblemDetail .forStatus(badRequestStatus)
55+ problemDetail.type = URI .create(httpStatusCodeTypePrefix + badRequestStatus.value())
56+ val globalErrors = BindErrorUtils .resolveAndJoin(exception.globalErrors)
57+ val fieldErrors = BindErrorUtils .resolveAndJoin(exception.fieldErrors)
58+ if ( globalErrors.isBlank() && fieldErrors.isBlank() ) {
59+ problemDetail.detail = exception.message
60+ } else {
61+ problemDetail.detail = " $globalErrors $fieldErrors " .trim()
62+ }
63+
64+ return super .handleExceptionInternal(exception, problemDetail, headers, status, request)
65+ }
66+
4567 @ExceptionHandler
4668 fun handleIllegalArgumentException (exception : IllegalArgumentException ): ProblemDetail {
4769 val badRequestStatus = HttpStatus .BAD_REQUEST
0 commit comments