Skip to content

Commit e85ae3a

Browse files
committed
Improving the exceptions thrown by methods in UniquenessValidationUtils.
1 parent 50e8afa commit e85ae3a

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
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.combineExceptions(exceptions));
23+
+ ExceptionUtils.combineMessages(exceptions));
2424
}
2525
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ 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+
1634
/**
1735
* Creates a string containing multiple exception messages.
1836
*

src/test/groovy/edu/ie3/datamodel/utils/validation/UniquenessValidationUtilsTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class UniquenessValidationUtilsTest extends Specification {
9898
then:
9999
DuplicateEntitiesException de = thrown()
100100
de.message == "The following exception(s) occurred while checking the uniqueness of 'AssetInput' entities: \n" +
101-
" edu.ie3.datamodel.exceptions.DuplicateEntitiesException: 'DummyAssetInput' entities with duplicated String key, but different field values found! Affected primary keys: [first]"
101+
" 'DummyAssetInput' entities with duplicated String key, but different field values found! Affected primary keys: [first]"
102102
}
103103

104104
def "Checking if result entities are unique"() {

0 commit comments

Comments
 (0)