Extend ExceptionInfo with map info + EntityNotFound info (#1901)

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-10-17 16:51:29 +03:00
committed by GitHub
parent 331cf8e692
commit 60ee383158
5 changed files with 75 additions and 110 deletions

View File

@@ -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<String, Object> 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<String, Object>) null);
}
/**
* Parameterized constructor.
*
* @param message custom error message
* @param error detail
*/
protected AbstractServerRtException(final String message, final SpServerError error, final Map<String, Object> 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<String, Object> info) {
super(message, cause);
this.error = error;
this.info = info;
}
/**

View File

@@ -11,6 +11,7 @@ package org.eclipse.hawkbit.repository.exception;
import java.io.Serial;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.Getter;
@@ -25,15 +26,15 @@ import org.eclipse.hawkbit.repository.model.MetaData;
@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 @@ public class EntityNotFoundException extends AbstractServerRtException {
* @param entityId of the {@link BaseEntity}
*/
public EntityNotFoundException(final Class<? extends BaseEntity> 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 class EntityNotFoundException extends AbstractServerRtException {
*/
public EntityNotFoundException(final Class<? extends MetaData> 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 class EntityNotFoundException extends AbstractServerRtException {
* @param key for the {@link MetaData} entry
*/
public EntityNotFoundException(final Class<? extends MetaData> 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 class EntityNotFoundException extends AbstractServerRtException {
*/
public EntityNotFoundException(final Class<? extends BaseEntity> 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)));
}
}

View File

@@ -268,9 +268,8 @@ public class ResponseExceptionHandler {
response.setExceptionClass(ex.getClass().getName());
if (ex instanceof AbstractServerRtException) {
response.setErrorCode(((AbstractServerRtException) ex).getError().getKey());
response.setInfo(((AbstractServerRtException) ex).getInfo());
}
return response;
}
}
}

View File

@@ -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<String> 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<String> getParameters() {
return parameters;
}
/**
* @param parameters
* the parameters to set
*/
public void setParameters(final List<String> 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<String, Object> info;
}

View File

@@ -11,8 +11,8 @@ package org.eclipse.hawkbit.rest.json.model;
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 class ExceptionInfoTest {
final String knownExceptionClass = "hawkbit.test.exception.Class";
final String knownErrorCode = "hawkbit.error.code.Known";
final String knownMessage = "a known message";
final List<String> knownParameters = new ArrayList<>();
knownParameters.add("param1");
knownParameters.add("param2");
final Map<String, Object> 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 class ExceptionInfoTest {
.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);
}
}
}