From a988be457167ecce616a3e8097e85f4bb9293be5 Mon Sep 17 00:00:00 2001 From: William Welling Date: Mon, 22 Jan 2024 08:25:30 -0600 Subject: [PATCH] Add ConstraintViolationException handler to custom response entity exception handler --- .../advice/CustomResponseEntityExceptionHandler.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/org/tdl/vireo/controller/advice/CustomResponseEntityExceptionHandler.java b/src/main/java/org/tdl/vireo/controller/advice/CustomResponseEntityExceptionHandler.java index 9d22a5923..6a2c04b6f 100644 --- a/src/main/java/org/tdl/vireo/controller/advice/CustomResponseEntityExceptionHandler.java +++ b/src/main/java/org/tdl/vireo/controller/advice/CustomResponseEntityExceptionHandler.java @@ -5,6 +5,7 @@ import javax.persistence.EntityNotFoundException; +import org.hibernate.exception.ConstraintViolationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -44,6 +45,15 @@ public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptio @Value("${spring.servlet.multipart.max-file-size:20MB}") private String maxFileSize; + @ExceptionHandler(ConstraintViolationException.class) + @ResponseStatus(value = HttpStatus.CONFLICT) + @ResponseBody + public ApiResponse handleConstraintViolationException(ConstraintViolationException exception) { + logger.error(exception.getMessage()); + logger.debug(exception.getMessage(), exception); + return new ApiResponse(ERROR, exception.getMessage()); + } + @ExceptionHandler(DataIntegrityViolationException.class) @ResponseStatus(value = HttpStatus.CONFLICT) @ResponseBody