From 679f465db1739a53a903d458d7a0abb678e36cc2 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Wed, 24 Aug 2016 13:07:37 +0200 Subject: [PATCH] Insert custom constraintViolationException Signed-off-by: Melanie Retter --- .../hawkbit/exception/SpServerError.java | 4 +++ .../ConstraintViolationException.java | 33 +++++++++++++++++++ .../exception/ResponseExceptionHandler.java | 16 +++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java index 29ab123e5..bda7a5d77 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java @@ -21,6 +21,10 @@ public enum SpServerError { * */ SP_REPO_ENTITY_ALRREADY_EXISTS("hawkbit.server.error.repo.entitiyAlreayExists", "The given entity already exists in database"), + /** + * + */ + SP_REPO_CONSTRAINT_VIOLATION("hawkbit.server.error.repo.constraintViolation", "The given entity cannot be saved due to Constraint Violation"), /** * */ diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java new file mode 100644 index 000000000..7e1564e3d --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.repository.exception; + +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 { + + private static final long serialVersionUID = 1L; + + /** + * Constructor for {@link ConstraintViolationException} + * + * @param message + * the message to be displayed as exception message + */ + public ConstraintViolationException(final String message) { + super(message, SpServerError.SP_REPO_CONSTRAINT_VIOLATION); + } + +} diff --git a/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java b/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java index 0c9f7cf68..89beaa4d7 100644 --- a/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java +++ b/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java @@ -8,11 +8,14 @@ */ 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 javax.servlet.http.HttpServletRequest; +import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -137,8 +140,17 @@ public class ResponseExceptionHandler { public ResponseEntity handleConstraintViolationException(final HttpServletRequest request, final Exception ex) { logRequest(request, ex); - final ExceptionInfo response = createExceptionInfo( - new ConstraintViolationException(((ConstraintViolationException) ex).getConstraintViolations())); + + final ExceptionInfo response = null; + final Set> violations = ((ConstraintViolationException) ex).getConstraintViolations(); + + final List messages = new ArrayList<>(); + violations.stream() + .forEach(violation -> messages.add(violation.getPropertyPath() + " " + violation.getMessage() + ". ")); + + // response = createExceptionInfo(new + // org.eclipse.hawkbit.repository.exception.ConstraintViolationException( + // messages.forEach(StringBuilder::append(this)))); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); }