Skip to content

Commit 077a760

Browse files
committed
Removing exception class information from combined exceptions.
1 parent b60c803 commit 077a760

15 files changed

+62
-78
lines changed

src/main/java/edu/ie3/datamodel/exceptions/DuplicateEntitiesException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public DuplicateEntitiesException(
2020
"The following exception(s) occurred while checking the uniqueness of '"
2121
+ entityName
2222
+ "' entities: \n"
23-
+ ExceptionUtils.combineMessages(exceptions));
23+
+ ExceptionUtils.combineExceptions(exceptions));
2424
}
2525
}

src/main/java/edu/ie3/datamodel/exceptions/FailedValidationException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public FailedValidationException(String message) {
2323

2424
/** @param exceptions List of exceptions, which must not be empty */
2525
public FailedValidationException(List<? extends Exception> exceptions) {
26-
super("Validation failed due to: " + ExceptionUtils.combineExceptions(exceptions));
26+
super("Validation failed due to:\n " + ExceptionUtils.combineExceptions(exceptions));
2727
}
2828
}

src/main/java/edu/ie3/datamodel/io/factory/Factory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Try<R, FactoryException> get(D data) {
7373
* {@link Failure}
7474
*/
7575
public Try<R, FactoryException> get(Try<D, ?> data) {
76-
return data.transformF(FactoryException::new).flatMap(this::get);
76+
return data.transformF(e -> new FactoryException(e.getMessage(), e)).flatMap(this::get);
7777
}
7878

7979
/**

src/main/java/edu/ie3/datamodel/io/source/EntitySource.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@ protected static <E extends Entity, D extends EntityData> Stream<E> getEntities(
205205
*/
206206
protected static Stream<Try<EntityData, SourceException>> buildEntityData(
207207
Class<? extends Entity> entityClass, DataSource dataSource) throws SourceException {
208-
Stream<Map<String, String>> dataStream =
209-
Try.of(() -> dataSource.getSourceData(entityClass), SourceException.class).getOrThrow();
210-
211-
return dataStream.map(
212-
fieldsToAttributes -> new Try.Success<>(new EntityData(fieldsToAttributes, entityClass)));
208+
return dataSource
209+
.getSourceData(entityClass)
210+
.map(
211+
fieldsToAttributes ->
212+
new Try.Success<>(new EntityData(fieldsToAttributes, entityClass)));
213213
}
214214

215215
/**
@@ -373,16 +373,14 @@ protected static <E extends EntityData, R> Try<R, SourceException> extractFuncti
373373
return entityData.flatMap(
374374
data ->
375375
Try.of(() -> data.getUUID(fieldName), FactoryException.class)
376+
.flatMap(entityUuid -> extractFunction(entityUuid, entities))
376377
.transformF(
377378
exception ->
378379
new SourceException(
379-
"Extracting UUID field "
380+
"Extracting UUID for field '"
380381
+ fieldName
381-
+ " from entity data "
382-
+ entityData
383-
+ " failed.",
384-
exception))
385-
.flatMap(entityUuid -> extractFunction(entityUuid, entities)));
382+
+ "' failed. Caused by: "
383+
+ exception.getMessage())));
386384
}
387385

388386
/**
@@ -393,13 +391,13 @@ protected static <E extends EntityData, R> Try<R, SourceException> extractFuncti
393391
* @return a try of the {@link Entity}
394392
* @param <T> type of entity
395393
*/
396-
protected static <T> Try<T, SourceException> extractFunction(UUID uuid, Map<UUID, T> entityMap) {
394+
protected static <T> Try<T, FactoryException> extractFunction(UUID uuid, Map<UUID, T> entityMap) {
397395
return Optional.ofNullable(entityMap.get(uuid))
398396
// We either find a matching entity for given UUID, thus return a success
399-
.map(entity -> Try.of(() -> entity, SourceException.class))
397+
.map(entity -> Try.of(() -> entity, FactoryException.class))
400398
// ... or find no matching entity, returning a failure.
401399
.orElse(
402-
new Failure<>(new SourceException("Entity with uuid " + uuid + " was not provided.")));
400+
new Failure<>(new FactoryException("Entity with uuid " + uuid + " was not provided.")));
403401
}
404402

405403
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

src/main/java/edu/ie3/datamodel/io/source/GraphicSource.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ public GraphicElements getGraphicElements(Map<UUID, NodeInput> nodes, Map<UUID,
9999

100100
if (!exceptions.isEmpty()) {
101101
throw new GraphicSourceException(
102-
"Some exception(s) occurred while initializing graphic elements. ", exceptions);
102+
"Exception(s) occurred in "
103+
+ exceptions.size()
104+
+ " input file(s) while initializing graphic elements. ",
105+
exceptions);
103106
} else {
104107
// if everything is fine, return a GraphicElements instance
105108
// getOrThrow should not throw an exception in this context, because all exception are

src/main/java/edu/ie3/datamodel/io/source/RawGridSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public RawGridElements getGridData(
160160
throw new RawGridException(
161161
"Exception(s) occurred in "
162162
+ exceptions.size()
163-
+ " input files while initializing raw grid.",
163+
+ " input file(s) while initializing raw grid.",
164164
exceptions);
165165
} else {
166166
/* build and return the grid if it is not empty */

src/main/java/edu/ie3/datamodel/io/source/SystemParticipantSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public SystemParticipants getSystemParticipants(
227227
throw new SystemParticipantsException(
228228
"Exception(s) occurred in "
229229
+ exceptions.size()
230-
+ " input files while initializing system participants.",
230+
+ " input file(s) while initializing system participants.",
231231
exceptions);
232232
} else {
233233
// if everything is fine, return a system participants container

src/main/java/edu/ie3/datamodel/utils/ExceptionUtils.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@ private ExceptionUtils() {
1313
throw new IllegalStateException("Utility classes cannot be instantiated");
1414
}
1515

16-
/**
17-
* Creates a string containing multiple exception messages.
18-
*
19-
* @param exceptions list of exceptions
20-
* @return str containing the messages
21-
*/
22-
public static String combineMessages(List<? extends Exception> exceptions) {
23-
String messageSeparator = "\n ";
24-
25-
String messages =
26-
exceptions.stream()
27-
.map(Throwable::getMessage)
28-
.reduce("", (a, b) -> a + messageSeparator + b);
29-
30-
// some formating
31-
return messages.replace("\n", "\n ").replaceFirst(messageSeparator, "");
32-
}
33-
3416
/**
3517
* Creates a string containing multiple exception messages.
3618
*
@@ -43,17 +25,18 @@ public static String combineExceptions(List<? extends Exception> exceptions) {
4325
// function to convert an exception into a string
4426
Function<Exception, String> converter =
4527
e -> {
46-
String clazz = e.getClass().getName();
4728
String message = e.getMessage();
4829
Throwable cause = e.getCause();
4930

50-
String res = clazz + ": " + message;
51-
5231
if (cause != null) {
53-
res += " Caused by: " + cause.getClass().getName() + ": " + cause.getMessage();
32+
String causeMessage = cause.getMessage();
33+
34+
if (!message.equalsIgnoreCase(causeMessage)) {
35+
message += " Caused by: " + cause.getMessage();
36+
}
5437
}
5538

56-
return res;
39+
return message;
5740
};
5841

5942
String messages =

src/test/groovy/edu/ie3/datamodel/io/source/EnergyManagementSourceTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class EnergyManagementSourceTest extends Specification {
225225
then:
226226
def exc = thrown(SourceException)
227227
exc.message == "1 exception(s) occurred within \"EmInput\" data: \n" +
228-
" edu.ie3.datamodel.exceptions.FactoryException: An error occurred when creating instance of EmInput.class. Caused by: edu.ie3.datamodel.exceptions.FactoryException: Field \"id\" not found in EntityData"
228+
" An error occurred when creating instance of EmInput.class. Caused by: Field \"id\" not found in EntityData"
229229
}
230230

231231
def "An EnergyManagementSource should fail if a parent em is not provided"() {

src/test/groovy/edu/ie3/datamodel/io/source/EntitySourceTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class EntitySourceTest extends Specification {
4949
then:
5050
SourceException ex = thrown()
5151
ex.message == "1 exception(s) occurred within \"OperatorInput\" data: \n" +
52-
" edu.ie3.datamodel.exceptions.FactoryException: An error occurred when creating instance of OperatorInput.class. Caused by: edu.ie3.datamodel.exceptions.FactoryException: Field \"id\" not found in EntityData"
52+
" An error occurred when creating instance of OperatorInput.class. Caused by: Field \"id\" not found in EntityData"
5353
}
5454

5555
def "An EntitySource can build EntityData correctly"() {
@@ -169,12 +169,12 @@ class EntitySourceTest extends Specification {
169169
then:
170170
actual.failure
171171
actual.exception.get().class == SourceException
172-
actual.exception.get().message.contains(expectedMessage)
172+
actual.exception.get().message == expectedMessage
173173

174174
where:
175175
fieldsToAttributes | entityMap | expectedMessage
176-
["operator": "no uuid"] | map([OperatorInput.NO_OPERATOR_ASSIGNED]) | "Extracting UUID field operator from entity data"
177-
["operator": GridTestData.profBroccoli.uuid.toString()] | map([OperatorInput.NO_OPERATOR_ASSIGNED]) | "Entity with uuid f15105c4-a2de-4ab8-a621-4bc98e372d92 was not provided."
176+
["operator": "no uuid"] | map([OperatorInput.NO_OPERATOR_ASSIGNED]) | "Extracting UUID for field 'operator' failed. Caused by: Exception while trying to parse UUID of field \"operator\" with value \"no uuid\""
177+
["operator": GridTestData.profBroccoli.uuid.toString()] | map([OperatorInput.NO_OPERATOR_ASSIGNED]) | "Extracting UUID for field 'operator' failed. Caused by: Entity with uuid f15105c4-a2de-4ab8-a621-4bc98e372d92 was not provided."
178178
}
179179

180180
def "An EntitySource returns a failure if a given map does not contain the given uuid"() {

0 commit comments

Comments
 (0)