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;
|
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.AbstractServerRtException;
|
||||||
import org.eclipse.hawkbit.exception.SpServerError;
|
import org.eclipse.hawkbit.exception.SpServerError;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the {@link ConstraintViolationException} is thrown when an entity is tried to
|
* the {@link ConstraintViolationException} is thrown when an entity is tried to
|
||||||
* be saved which has constraint violations
|
* be saved which has constraint violations
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ConstraintViolationException extends AbstractServerRtException {
|
public class ConstraintViolationException extends AbstractServerRtException {
|
||||||
|
|
||||||
@@ -23,11 +29,21 @@ public class ConstraintViolationException extends AbstractServerRtException {
|
|||||||
/**
|
/**
|
||||||
* Constructor for {@link ConstraintViolationException}
|
* Constructor for {@link ConstraintViolationException}
|
||||||
*
|
*
|
||||||
* @param message
|
* @param ex
|
||||||
* the message to be displayed as exception message
|
* the javax.validation.ConstraintViolationException which is
|
||||||
|
* thrown
|
||||||
*/
|
*/
|
||||||
public ConstraintViolationException(final String message) {
|
public ConstraintViolationException(final javax.validation.ConstraintViolationException ex) {
|
||||||
super(message, SpServerError.SP_REPO_CONSTRAINT_VIOLATION);
|
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;
|
package org.eclipse.hawkbit.rest.exception;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.validation.ConstraintViolation;
|
|
||||||
import javax.validation.ConstraintViolationException;
|
import javax.validation.ConstraintViolationException;
|
||||||
|
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
@@ -142,15 +138,9 @@ public class ResponseExceptionHandler {
|
|||||||
final Exception ex) {
|
final Exception ex) {
|
||||||
logRequest(request, 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(
|
final ExceptionInfo response = createExceptionInfo(
|
||||||
new org.eclipse.hawkbit.repository.exception.ConstraintViolationException(
|
new org.eclipse.hawkbit.repository.exception.ConstraintViolationException(
|
||||||
messages.stream().collect(Collectors.joining(" "))));
|
(ConstraintViolationException) ex));
|
||||||
|
|
||||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user