Skip to content

Commit 6681218

Browse files
committed
Override handleMethodArgumentNotValid
By doing this, the Cosmotech API will return clear error messages when you call endpoints with not valid arguments (in addition with field's constraints on OpenAPI side)
1 parent fca1b2a commit 6681218

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,8 @@ val awaitilityKVersion = "4.2.2"
175175
val testcontainersRedis = "1.6.4"
176176

177177
dependencies {
178-
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)) {
179-
}
180-
178+
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
179+
implementation("net.minidev:json-smart:2.4.10")
181180

182181
detekt("io.gitlab.arturbosch.detekt:detekt-cli:$detektVersion")
183182
detekt("io.gitlab.arturbosch.detekt:detekt-formatting:$detektVersion")

src/main/kotlin/com/cosmotech/api/exceptions/CsmExceptionHandling.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import org.springframework.http.converter.HttpMessageNotReadableException
1515
import org.springframework.security.authentication.AuthenticationServiceException
1616
import org.springframework.security.authentication.BadCredentialsException
1717
import org.springframework.security.authentication.InsufficientAuthenticationException
18+
import org.springframework.web.bind.MethodArgumentNotValidException
1819
import org.springframework.web.bind.annotation.ExceptionHandler
1920
import org.springframework.web.bind.annotation.RestControllerAdvice
2021
import org.springframework.web.context.request.WebRequest
2122
import 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

Comments
 (0)