Skip to content

Commit

Permalink
Add ConstraintViolationException handler to custom response entity ex…
Browse files Browse the repository at this point in the history
…ception handler
  • Loading branch information
wwelling committed Jan 22, 2024
1 parent f024240 commit a988be4
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a988be4

Please sign in to comment.