diff --git a/build.gradle.kts b/build.gradle.kts index 4b133b82..405b34af 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -175,9 +175,8 @@ val awaitilityKVersion = "4.2.2" val testcontainersRedis = "1.6.4" dependencies { - implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)) { - } - + implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)) + implementation("net.minidev:json-smart:2.4.10") detekt("io.gitlab.arturbosch.detekt:detekt-cli:$detektVersion") detekt("io.gitlab.arturbosch.detekt:detekt-formatting:$detektVersion") diff --git a/src/main/kotlin/com/cosmotech/api/exceptions/CsmExceptionHandling.kt b/src/main/kotlin/com/cosmotech/api/exceptions/CsmExceptionHandling.kt index f72c91c7..3ed7f490 100644 --- a/src/main/kotlin/com/cosmotech/api/exceptions/CsmExceptionHandling.kt +++ b/src/main/kotlin/com/cosmotech/api/exceptions/CsmExceptionHandling.kt @@ -15,10 +15,12 @@ import org.springframework.http.converter.HttpMessageNotReadableException import org.springframework.security.authentication.AuthenticationServiceException import org.springframework.security.authentication.BadCredentialsException import org.springframework.security.authentication.InsufficientAuthenticationException +import org.springframework.web.bind.MethodArgumentNotValidException import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.RestControllerAdvice import org.springframework.web.context.request.WebRequest import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler +import org.springframework.web.util.BindErrorUtils @Order(Ordered.HIGHEST_PRECEDENCE) @RestControllerAdvice @@ -42,6 +44,26 @@ open class CsmExceptionHandling : ResponseEntityExceptionHandler() { return super.handleExceptionInternal(exception, problemDetail, headers, status, request) } + override fun handleMethodArgumentNotValid( + exception: MethodArgumentNotValidException, + headers: HttpHeaders, + status: HttpStatusCode, + request: WebRequest + ): ResponseEntity? { + val badRequestStatus = HttpStatus.BAD_REQUEST + val problemDetail = ProblemDetail.forStatus(badRequestStatus) + problemDetail.type = URI.create(httpStatusCodeTypePrefix + badRequestStatus.value()) + val globalErrors = BindErrorUtils.resolveAndJoin(exception.globalErrors) + val fieldErrors = BindErrorUtils.resolveAndJoin(exception.fieldErrors) + if( globalErrors.isBlank() && fieldErrors.isBlank() ) { + problemDetail.detail = exception.message + } else { + problemDetail.detail = "$globalErrors $fieldErrors".trim() + } + + return super.handleExceptionInternal(exception, problemDetail, headers, status, request) + } + @ExceptionHandler fun handleIllegalArgumentException(exception: IllegalArgumentException): ProblemDetail { val badRequestStatus = HttpStatus.BAD_REQUEST