Skip to content

Commit 8192a49

Browse files
Merge pull request #712 from ie3-institute/ms/#707-Refactor-data-sources-to-throw-exceptions-instead-of-returning-Optionals
Refactor data source to throw exception instead of returning optionals
2 parents d6f8c59 + 14e458a commit 8192a49

File tree

82 files changed

+2510
-1430
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2510
-1430
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Refactor CSV data sources [#716](https://github.com/ie3-institute/PowerSystemDataModel/issues/716)
2222
- Deleted parameter initFiles, set parameter append to false by default [#791](https://github.com/ie3-institute/PowerSystemDataModel/issues/791)
2323
- Use nio paths instead of strings for file path [#723](https://github.com/ie3-institute/PowerSystemDataModel/issues/723)
24+
- Data source will throw an exceptions instead of returning an empty optionals [#707](https://github.com/ie3-institute/PowerSystemDataModel/issues/707)
2425

2526

2627
## [3.0.0] - 2023-02-16
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* © 2023. TU Dortmund University,
3+
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
4+
* Research group Distribution grid planning and operation
5+
*/
6+
package edu.ie3.datamodel.exceptions;
7+
8+
public class FailureException extends Exception {
9+
public FailureException(String message, Throwable throwable) {
10+
super(message, throwable);
11+
}
12+
13+
public FailureException(String message) {
14+
super(message);
15+
}
16+
17+
public FailureException(Throwable throwable) {
18+
super(throwable);
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* © 2023. TU Dortmund University,
3+
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
4+
* Research group Distribution grid planning and operation
5+
*/
6+
package edu.ie3.datamodel.exceptions;
7+
8+
import java.util.List;
9+
10+
public class GraphicSourceException extends SourceException {
11+
public GraphicSourceException(String message, List<SourceException> exceptions) {
12+
super(message, exceptions);
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* © 2023. TU Dortmund University,
3+
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
4+
* Research group Distribution grid planning and operation
5+
*/
6+
package edu.ie3.datamodel.exceptions;
7+
8+
import java.util.List;
9+
10+
public class RawGridException extends SourceException {
11+
public RawGridException(String message, List<SourceException> exceptions) {
12+
super(message, exceptions);
13+
}
14+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
package edu.ie3.datamodel.exceptions;
77

8+
import edu.ie3.datamodel.utils.ExceptionUtils;
9+
import java.util.List;
10+
811
/**
912
* Exception that should be used whenever an error occurs in a instance of a {@link
1013
* edu.ie3.datamodel.io.source.DataSource}
@@ -27,4 +30,8 @@ public SourceException(final Throwable cause) {
2730
public SourceException(final String message) {
2831
super(message);
2932
}
33+
34+
public SourceException(String message, List<? extends Exception> exceptions) {
35+
super(message + " " + ExceptionUtils.getMessages(exceptions), exceptions.get(0));
36+
}
3037
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* © 2023. TU Dortmund University,
3+
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
4+
* Research group Distribution grid planning and operation
5+
*/
6+
package edu.ie3.datamodel.exceptions;
7+
8+
import java.util.List;
9+
10+
public class SystemParticipantsException extends SourceException {
11+
public SystemParticipantsException(String message, List<SourceException> exceptions) {
12+
super(message, exceptions);
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* © 2023. TU Dortmund University,
3+
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
4+
* Research group Distribution grid planning and operation
5+
*/
6+
package edu.ie3.datamodel.exceptions;
7+
8+
public class TryException extends RuntimeException {
9+
public TryException(String message, Throwable throwable) {
10+
super(message, throwable);
11+
}
12+
}

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package edu.ie3.datamodel.io.factory;
77

88
import edu.ie3.datamodel.exceptions.FactoryException;
9+
import edu.ie3.datamodel.utils.Try;
10+
import edu.ie3.datamodel.utils.Try.*;
911
import java.util.*;
1012
import java.util.function.IntFunction;
1113
import java.util.stream.Collectors;
@@ -39,27 +41,40 @@ public List<Class<? extends C>> getSupportedClasses() {
3941
* data
4042
*
4143
* @param data EntityData (or subclass) containing the data
42-
* @return An entity wrapped in Option if successful, an empty option otherwise
44+
* @return An entity wrapped in a {@link Success} if successful, or an exception wrapped in a
45+
* {@link Failure}
4346
*/
44-
public Optional<R> get(D data) {
47+
public Try<R, FactoryException> get(D data) {
4548
isSupportedClass(data.getTargetClass());
4649

4750
// magic: case-insensitive get/set calls on set strings
4851
final List<Set<String>> allFields = getFields(data);
4952

50-
validateParameters(data, allFields.toArray((IntFunction<Set<String>[]>) Set[]::new));
51-
5253
try {
54+
validateParameters(data, allFields.toArray((IntFunction<Set<String>[]>) Set[]::new));
55+
5356
// build the model
54-
return Optional.of(buildModel(data));
57+
return new Success<>(buildModel(data));
5558
} catch (FactoryException e) {
56-
// only catch FactoryExceptions, as more serious exceptions should be handled elsewhere
57-
log.error(
58-
"An error occurred when creating instance of {}.class.",
59-
data.getTargetClass().getSimpleName(),
60-
e);
59+
return new Failure<>(
60+
new FactoryException(
61+
"An error occurred when creating instance of "
62+
+ data.getTargetClass().getSimpleName()
63+
+ ".class.",
64+
e));
6165
}
62-
return Optional.empty();
66+
}
67+
68+
/**
69+
* Builds entity with data from given EntityData object after doing all kinds of checks on the
70+
* data
71+
*
72+
* @param data EntityData (or subclass) containing the data wrapped in a {@link Try}
73+
* @return An entity wrapped in a {@link Success} if successful, or an exception wrapped in a
74+
* {@link Failure}
75+
*/
76+
public Try<R, FactoryException> get(Try<D, ?> data) {
77+
return data.transformF(FactoryException::new).flatMap(this::get);
6378
}
6479

6580
/**

0 commit comments

Comments
 (0)