This is my exception handler
@ControllerAdvice(basePackageClasses = [SignUpController::class])
class ControllerAdvice: ResponseEntityExceptionHandler() {
@ExceptionHandler(Exception::class)
@ResponseBody
fun handleControllerException(request: HttpServletRequest, ex: Throwable): ResponseEntity<*> {
val status = HttpStatus.CONFLICT
return ResponseEntity<Any>(ApiError(status.value(), ex.message), status)
}
}
and my custom class
data class ApiError(val status: Int, val message: String?)
The handler work but error is throw like follow
{
"timestamp": "2019-01-29T19:17:22.541+0000",
"status": 401,
"error": "Unauthorized",
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
"path": "/sign-up"
}
But I'm expected for something like follow
{
"apierror": {
"status": ...,
"message": ..."
}
}
I'm based for this tutorials
https://www.toptal.com/java/spring-boot-rest-api-error-handling https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling https://medium.com/@jovannypcg/understanding-springs-controlleradvice-cd96a364033f
What I'm doing wrong? I'm missing any config?