diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/AbstractServerRtException.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/AbstractServerRtException.java index 38f591250f..9ffdd557fa 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/AbstractServerRtException.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/AbstractServerRtException.java @@ -9,65 +9,94 @@ */ package org.eclipse.hawkbit.exception; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.Serial; +import java.util.Map; + /** * Generic Custom Exception to wrap the Runtime and checked exception */ +@Data +@EqualsAndHashCode(callSuper = true) public abstract class AbstractServerRtException extends RuntimeException { + @Serial private static final long serialVersionUID = 1L; private final SpServerError error; + private final Map info; /** * Parameterized constructor. * - * @param error - * detail + * @param error detail */ protected AbstractServerRtException(final SpServerError error) { super(error.getMessage()); this.error = error; + this.info = null; } /** * Parameterized constructor. * - * @param message - * custom error message - * @param error - * detail + * @param message custom error message + * @param error detail */ protected AbstractServerRtException(final String message, final SpServerError error) { + this(message, error, (Map) null); + } + + /** + * Parameterized constructor. + * + * @param message custom error message + * @param error detail + */ + protected AbstractServerRtException(final String message, final SpServerError error, final Map info) { super(message); this.error = error; + this.info = info; } /** * Parameterized constructor. * - * @param message - * custom error message - * @param error - * detail - * @param cause - * of the exception + * @param message custom error message + * @param error detail + * @param cause of the exception */ protected AbstractServerRtException(final String message, final SpServerError error, final Throwable cause) { super(message, cause); this.error = error; + this.info = null; } /** * Parameterized constructor. * - * @param error - * detail - * @param cause - * of the exception + * @param error detail + * @param cause of the exception */ protected AbstractServerRtException(final SpServerError error, final Throwable cause) { super(error.getMessage(), cause); this.error = error; + this.info = null; + } + + /** + * Parameterized constructor. + * + * @param message custom error message + * @param error detail + * @param cause of the exception + */ + protected AbstractServerRtException(final String message, final SpServerError error, final Throwable cause, final Map info) { + super(message, cause); + this.error = error; + this.info = info; } /** diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/EntityNotFoundException.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/EntityNotFoundException.java index b52d50078f..68a21e211d 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/EntityNotFoundException.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/EntityNotFoundException.java @@ -11,6 +11,7 @@ import java.io.Serial; import java.util.Collection; +import java.util.Map; import java.util.stream.Collectors; import lombok.Getter; @@ -25,15 +26,15 @@ @Getter public class EntityNotFoundException extends AbstractServerRtException { + public static final String KEY = "key"; + public static final String ENTITY_ID = "entityId"; + public static final String TYPE = "type"; + @Serial private static final long serialVersionUID = 1L; private static final SpServerError THIS_ERROR = SpServerError.SP_REPO_ENTITY_NOT_EXISTS; - private Class type; - private Object entityId; - private String key; - /** * Default constructor. */ @@ -76,9 +77,9 @@ protected EntityNotFoundException(final String message) { * @param entityId of the {@link BaseEntity} */ public EntityNotFoundException(final Class type, final Object entityId) { - super(type.getSimpleName() + " with given identifier {" + entityId + "} does not exist.", THIS_ERROR); - this.type = type; - this.entityId = entityId; + super(type.getSimpleName() + " with given identifier {" + entityId + "} does not exist.", + THIS_ERROR, + Map.of(TYPE, type.getSimpleName(), ENTITY_ID, entityId)); } /** @@ -90,9 +91,6 @@ public EntityNotFoundException(final Class type, final Obj */ public EntityNotFoundException(final Class type, final Long entityId, final String key) { this(type, String.valueOf(entityId), key); - this.type = type; - this.entityId = entityId; - this.key = key; } /** @@ -103,10 +101,9 @@ public EntityNotFoundException(final Class type, final Long * @param key for the {@link MetaData} entry */ public EntityNotFoundException(final Class type, final String entityId, final String key) { - this(type.getSimpleName() + " for given entity {" + entityId + "} and with key {" + key + "} does not exist."); - this.type = type; - this.entityId = entityId; - this.key = key; + super(type.getSimpleName() + " for given entity {" + entityId + "} and with key {" + key + "} does not exist.", + THIS_ERROR, + Map.of(TYPE, type.getSimpleName(), ENTITY_ID, entityId, KEY, key)); } /** @@ -118,9 +115,9 @@ public EntityNotFoundException(final Class type, final Strin */ public EntityNotFoundException(final Class type, final Collection expected, final Collection found) { - this(type.getSimpleName() + "s with given identifiers {" + expected.stream().filter(id -> !found.contains(id)) - .map(String::valueOf).collect(Collectors.joining(",")) + "} do not exist."); - this.type = type; - this.entityId = expected.stream().filter(id -> !found.contains(id)).map(String::valueOf); + super(type.getSimpleName() + "s with given identifiers {" + expected.stream().filter(id -> !found.contains(id)) + .map(String::valueOf).collect(Collectors.joining(",")) + "} do not exist.", + THIS_ERROR, + Map.of(TYPE, type.getSimpleName(), ENTITY_ID, expected.stream().filter(id -> !found.contains(id)).map(String::valueOf))); } } \ No newline at end of file diff --git a/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java b/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java index 2102815bf2..bf7a0a5c2e 100644 --- a/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java +++ b/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java @@ -268,9 +268,8 @@ private ExceptionInfo createExceptionInfo(final Exception ex) { response.setExceptionClass(ex.getClass().getName()); if (ex instanceof AbstractServerRtException) { response.setErrorCode(((AbstractServerRtException) ex).getError().getKey()); + response.setInfo(((AbstractServerRtException) ex).getInfo()); } - return response; } - -} +} \ No newline at end of file diff --git a/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/json/model/ExceptionInfo.java b/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/json/model/ExceptionInfo.java index 3a6847a0fa..13582113d7 100644 --- a/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/json/model/ExceptionInfo.java +++ b/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/json/model/ExceptionInfo.java @@ -9,81 +9,22 @@ */ package org.eclipse.hawkbit.rest.json.model; -import java.util.List; +import java.util.Map; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; +import lombok.Data; /** * A exception model rest representation with JSON annotations for response * bodies in case of RESTful exception occurrence. - * */ +@Data @JsonInclude(Include.NON_EMPTY) public class ExceptionInfo { private String exceptionClass; private String errorCode; private String message; - private List parameters; - - /** - * @return the exceptionClass - */ - public String getExceptionClass() { - return exceptionClass; - } - - /** - * @param exceptionClass - * the exceptionClass to set - */ - public void setExceptionClass(final String exceptionClass) { - this.exceptionClass = exceptionClass; - } - - /** - * @return the parameters - */ - public List getParameters() { - return parameters; - } - - /** - * @param parameters - * the parameters to set - */ - public void setParameters(final List parameters) { - this.parameters = parameters; - } - - /** - * @return the errorCode - */ - public String getErrorCode() { - return errorCode; - } - - /** - * @param errorCode - * the errorCode to set - */ - public void setErrorCode(final String errorCode) { - this.errorCode = errorCode; - } - - /** - * @return the message - */ - public String getMessage() { - return message; - } - - /** - * @param message - * the message to set - */ - public void setMessage(final String message) { - this.message = message; - } + private Map info; } diff --git a/hawkbit-rest/hawkbit-rest-core/src/test/java/org/eclipse/hawkbit/rest/json/model/ExceptionInfoTest.java b/hawkbit-rest/hawkbit-rest-core/src/test/java/org/eclipse/hawkbit/rest/json/model/ExceptionInfoTest.java index 551d88eabd..3397210d5e 100644 --- a/hawkbit-rest/hawkbit-rest-core/src/test/java/org/eclipse/hawkbit/rest/json/model/ExceptionInfoTest.java +++ b/hawkbit-rest/hawkbit-rest-core/src/test/java/org/eclipse/hawkbit/rest/json/model/ExceptionInfoTest.java @@ -11,8 +11,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import org.junit.jupiter.api.Test; @@ -30,15 +30,15 @@ public void setterAndGetterOnExceptionInfo() { final String knownExceptionClass = "hawkbit.test.exception.Class"; final String knownErrorCode = "hawkbit.error.code.Known"; final String knownMessage = "a known message"; - final List knownParameters = new ArrayList<>(); - knownParameters.add("param1"); - knownParameters.add("param2"); + final Map knownInfo = new HashMap<>(); + knownInfo.put("param1", "1"); + knownInfo.put("param2", 2); final ExceptionInfo underTest = new ExceptionInfo(); underTest.setErrorCode(knownErrorCode); underTest.setExceptionClass(knownExceptionClass); underTest.setMessage(knownMessage); - underTest.setParameters(knownParameters); + underTest.setInfo(knownInfo); assertThat(underTest.getErrorCode()).as("The error code should match with the known error code in the test") .isEqualTo(knownErrorCode); @@ -47,8 +47,7 @@ public void setterAndGetterOnExceptionInfo() { .isEqualTo(knownExceptionClass); assertThat(underTest.getMessage()).as("The message should match with the known error code in the test") .isEqualTo(knownMessage); - assertThat(underTest.getParameters()).as("The parameters should match with the known error code in the test") - .isEqualTo(knownParameters); + assertThat(underTest.getInfo()).as("The parameters should match with the known error code in the test") + .isEqualTo(knownInfo); } - -} +} \ No newline at end of file