Refactor ConstraintViolationException
Signed-off-by: Melanie Retter <melanie.retter@bosch-si.com>
This commit is contained in:
@@ -8,13 +8,19 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.exception;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
|
||||
import org.eclipse.hawkbit.exception.AbstractServerRtException;
|
||||
import org.eclipse.hawkbit.exception.SpServerError;
|
||||
|
||||
/**
|
||||
* the {@link ConstraintViolationException} is thrown when an entity is tried to
|
||||
* be saved which has constraint violations
|
||||
*
|
||||
*/
|
||||
public class ConstraintViolationException extends AbstractServerRtException {
|
||||
|
||||
@@ -23,11 +29,21 @@ public class ConstraintViolationException extends AbstractServerRtException {
|
||||
/**
|
||||
* Constructor for {@link ConstraintViolationException}
|
||||
*
|
||||
* @param message
|
||||
* the message to be displayed as exception message
|
||||
* @param ex
|
||||
* the javax.validation.ConstraintViolationException which is
|
||||
* thrown
|
||||
*/
|
||||
public ConstraintViolationException(final String message) {
|
||||
super(message, SpServerError.SP_REPO_CONSTRAINT_VIOLATION);
|
||||
public ConstraintViolationException(final javax.validation.ConstraintViolationException ex) {
|
||||
super(setExceptionMessage(ex), SpServerError.SP_REPO_CONSTRAINT_VIOLATION);
|
||||
}
|
||||
|
||||
public static String setExceptionMessage(final javax.validation.ConstraintViolationException ex) {
|
||||
final Set<ConstraintViolation<?>> violations = ex.getConstraintViolations();
|
||||
final List<String> messages = new ArrayList<>();
|
||||
violations.stream()
|
||||
.forEach(violation -> messages.add(violation.getPropertyPath() + " " + violation.getMessage() + "."));
|
||||
|
||||
return messages.stream().collect(Collectors.joining(" "));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,15 +8,11 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.rest.exception;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
@@ -142,15 +138,9 @@ public class ResponseExceptionHandler {
|
||||
final Exception ex) {
|
||||
logRequest(request, ex);
|
||||
|
||||
final Set<ConstraintViolation<?>> violations = ((ConstraintViolationException) ex).getConstraintViolations();
|
||||
|
||||
final List<String> messages = new ArrayList<>();
|
||||
violations.stream()
|
||||
.forEach(violation -> messages.add(violation.getPropertyPath() + " " + violation.getMessage() + "."));
|
||||
|
||||
final ExceptionInfo response = createExceptionInfo(
|
||||
new org.eclipse.hawkbit.repository.exception.ConstraintViolationException(
|
||||
messages.stream().collect(Collectors.joining(" "))));
|
||||
(ConstraintViolationException) ex));
|
||||
|
||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user