2

I'm trying to implement an error handler in Spring with Kotlin, but it looks like the Application doesn't recognize it: i always get the /error page and the exception not handled.

@EnableWebMvc, suggested in other similar questions, didn't work for me.

This is my actual code:

@ControllerAdvice
class UserExceptionHandler {

     @ExceptionHandler(ConstraintViolationException::class)
     fun methodArgumentTypeMismatchException(e: ConstraintViolationException): ResponseEntity<*> {
         return ResponseEntity
            .status(HttpStatus.FORBIDDEN)
            .body("Constraints Involved. Pay Attention To The Parameters")
     }
}

The following is my @RestController:

@RestController
@RequestMapping("/api")
class UserController (@Autowired private val userRepository: UserRepository) {

     @PostMapping("/new-user")
     fun createNewUser(@RequestParam mailAddress: String,
                       @RequestParam password: String): ResponseEntity<UserEntity> =
         ResponseEntity.ok(userRepository.saveAndFlush(UserEntity(mailAddress = mailAddress, password = password)))
}

The UserEntity throws the Exception in case of not unique mails:

@Entity
data class UserEntity(
        @Id @NotBlank @GeneratedValue(strategy = GenerationType.IDENTITY)
        val id: Long? = null,

        @Column(unique=true) @NotBlank
        val mailAddress: String,

        @NotBlank
        var password: String
)

What am I doing wrong? The files I've shown share the same package.

EDIT: The following is the stack trace on the Spring LOG

ERROR 32916 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["UK_KQE6HHKTR4W4E02H0KN61F442_INDEX_F ON PUBLIC.USER_ENTITY(MAIL_ADDRESS) VALUES ('asd@lol.it', 97)"; SQL statement:
insert into user_entity (id, mail_address, password) values (null, ?, ?) [23505-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

WARN 33436 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Failure in @ExceptionHandler public org.springframework.http.ResponseEntity<?> unito.taas.project.user.UserExceptionHandler.methodArgumentTypeMismatchException(javax.validation.ConstraintViolationException)

java.lang.IllegalStateException: Could not resolve parameter [0] in public org.springframework.http.ResponseEntity<?> unito.taas.project.user.UserExceptionHandler.methodArgumentTypeMismatchException(javax.validation.ConstraintViolationException): No suitable resolver

ciurlaro
  • 742
  • 10
  • 22
  • Can you confirm that you actually validate argument and `ConstraintViolationException` being thrown on invalid object? Can you provide a stack trace when such situation had place? – Piotr Podraza Sep 19 '19 at 13:49
  • @PiotrPodraza I'm not sure I understood the question, but I can tell you that `ConstraintViolationException` is (on purpose, of course) thrown because of duplicated key. My problem is that `ControllerAdvice` doesn't manage to handle it and I don't know why. – ciurlaro Sep 19 '19 at 13:53

1 Answers1

3

You import ConstraintViolationException from the wrong package - javax.validation instead of org.hibernate.exception.

Make sure use use org.hibernate.exception.ConstraintViolationException in UserExceptionHandler.

Piotr Podraza
  • 1,941
  • 1
  • 15
  • 26