Skip to content

Commit

Permalink
Extend ExceptionInfo with map info + EntityNotFound info (#1901)
Browse files Browse the repository at this point in the history
Signed-off-by: Marinov Avgustin <[email protected]>
  • Loading branch information
avgustinmm authored Oct 17, 2024
1 parent 331cf8e commit 60ee383
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.Serial;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.Getter;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -76,9 +77,9 @@ protected EntityNotFoundException(final String message) {
* @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));
}

/**
Expand All @@ -90,9 +91,6 @@ public EntityNotFoundException(final Class<? extends BaseEntity> type, final Obj
*/
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;
}

/**
Expand All @@ -103,10 +101,9 @@ public EntityNotFoundException(final Class<? extends MetaData> type, final Long
* @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));
}

/**
Expand All @@ -118,9 +115,9 @@ public EntityNotFoundException(final Class<? extends MetaData> type, final Strin
*/
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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<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);
Expand All @@ -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);
}

}
}

0 comments on commit 60ee383

Please sign in to comment.