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(" "));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user