Hi, I'm using spring boot (v2.5.7) and kotlin (v1.5.31) with kotlinx.serialization (v1.3.1)
I proceeded with dto validation by referring to the link below. https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md#data-validation
My Example:
@Serializable
data class RequestEmailDto(
val email: String
) {
init {
// require(value: Boolean) -> Throws an "IllegalArgumentException" if the "value" is false.
require(email.isNotBlank()) { "The email you entered is blank or contains spaces." }
require(RegexConstants.isValidEmail(email)) { "It doesn't fit the email format." }
}
}
@RestControllerAdvice
class RestExceptionHandler: ResponseEntityExceptionHandler() {
@ExceptionHandler(*[IllegalArgumentException::class])
fun handleValidation(ex: IllegalArgumentException, request: WebRequest): ResponseEntity<Any> {
val errorResponse = ErrorResponse<Any>(
code = ex.code,
message = ex.message,
errors = null
)
return handleExceptionInternal(ex, errorResponse, HttpHeaders(), HttpStatus.BAD_REQUEST, request)
}
}
I was expecting it to be caught by the @ExceptionHandler, but it didn't.
Question: How to catch kotlin.require() exception using @RestControllerAdvice?
Thank you
Comment From: snicoll
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.