BAEL-3005 | updated exception handler
This commit is contained in:
parent
a178f09ef4
commit
98b5a7fdb7
@ -14,17 +14,23 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|||||||
|
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
public class ConstraintViolationExceptionHandler {
|
public class ConstraintViolationExceptionHandler {
|
||||||
private final Logger LOGGER = LoggerFactory.getLogger(ConstraintViolationExceptionHandler.class);
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(ConstraintViolationExceptionHandler.class);
|
||||||
|
|
||||||
@ExceptionHandler(ConstraintViolationException.class)
|
@ExceptionHandler(ConstraintViolationException.class)
|
||||||
public ResponseEntity<String> handle(ConstraintViolationException constraintViolationException) {
|
public ResponseEntity<String> handle(ConstraintViolationException constraintViolationException) {
|
||||||
Set<ConstraintViolation<?>> violations = constraintViolationException.getConstraintViolations();
|
Set<ConstraintViolation<?>> violations = constraintViolationException.getConstraintViolations();
|
||||||
|
String errorMessage = "";
|
||||||
|
if (!violations.isEmpty()) {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
for (ConstraintViolation<?> violation : violations) {
|
violations.forEach(violation -> builder.append("\n" + violation.getMessage()));
|
||||||
builder.append(violation.getMessage());
|
errorMessage = builder.toString();
|
||||||
|
} else {
|
||||||
|
errorMessage = "ConstraintViolationException occured.";
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.error(errorMessage);
|
||||||
|
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
LOGGER.error(builder.toString());
|
|
||||||
return new ResponseEntity<String>(builder.toString(), HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user