Skip to content

Refactor data source to throw exception instead of returning optionals #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
a3166e5
Refactor data source to throw exception instead of returning optionals
staudtMarius Dec 15, 2022
12a9821
Saving changes.
staudtMarius Jan 16, 2023
151b28f
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jan 19, 2023
452b68a
Adapting tests to changes.
staudtMarius Jan 19, 2023
7ba927e
Adding more information to error message.
staudtMarius Jan 23, 2023
38a7764
Adding more information to error message.
staudtMarius Jan 23, 2023
03f3833
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jan 26, 2023
feba1b0
fmt
staudtMarius Jan 26, 2023
3571fcb
Fixing codacy errors.
staudtMarius Jan 26, 2023
b1db068
Improving ``Try``-class.
staudtMarius Jan 30, 2023
5d9db21
Improving code.
staudtMarius Jan 31, 2023
a5b7c64
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jan 31, 2023
ca64b5a
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Feb 2, 2023
edd68a7
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Feb 14, 2023
19e41c7
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Feb 17, 2023
37ec8be
Changed CHANGELOG.
staudtMarius Feb 17, 2023
9204684
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Feb 20, 2023
7420284
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Feb 21, 2023
de5771b
Improving code.
staudtMarius Feb 21, 2023
78bca57
Improving code.
staudtMarius Feb 21, 2023
bd74326
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Feb 22, 2023
ef4a399
Improving code.
staudtMarius Feb 22, 2023
046d64b
Fixing tests.
staudtMarius Feb 22, 2023
bcac00f
Adding new functionality to Try object.
staudtMarius Mar 6, 2023
2bfa79d
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Mar 9, 2023
49cf841
Adding tests for ``Try`` classes.
staudtMarius Mar 9, 2023
c35ee81
fmt
staudtMarius Mar 11, 2023
4c5e25b
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
danielfeismann Mar 24, 2023
7a2c013
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
danielfeismann Apr 3, 2023
93bc6a8
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Apr 11, 2023
5d3361d
Implementing requested changes.
staudtMarius Apr 11, 2023
663f9fd
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Apr 18, 2023
1cd06be
fmt
staudtMarius Apr 18, 2023
a165640
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Apr 20, 2023
1fb3289
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Apr 24, 2023
26e4934
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Apr 24, 2023
3574dc1
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Apr 27, 2023
3899c6a
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius May 4, 2023
dadec39
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius May 9, 2023
c64aa32
fmt
staudtMarius May 9, 2023
9e04a80
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius May 15, 2023
21c39a3
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius May 16, 2023
79e8bdf
Adapting to changes.
staudtMarius May 16, 2023
f4785f1
fmt
staudtMarius May 16, 2023
6c1108e
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius May 22, 2023
b96dcc7
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius May 25, 2023
441b7a3
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius May 30, 2023
cede8d7
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jun 7, 2023
f0e2376
Improving ``Try`` implementation.
staudtMarius Jun 14, 2023
4ca5a14
Some improvements for ``Try`` class.
staudtMarius Jun 20, 2023
69714f5
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jun 20, 2023
e0ae3c0
Adapting to changes in ``dev`` branch.
staudtMarius Jun 20, 2023
5a25b00
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jun 26, 2023
eca39a0
fmt
staudtMarius Jun 26, 2023
a5f815c
fmt
staudtMarius Jun 26, 2023
ea4190c
fmt
staudtMarius Jun 26, 2023
e630048
Adapting ``JavaDoc`` to changes.
staudtMarius Jun 26, 2023
bf951ea
Fixing failing tests.
staudtMarius Jun 28, 2023
35db1d3
Fixing quality gate issues.
staudtMarius Jun 28, 2023
6c1828d
Fixing codacy issues.
staudtMarius Jun 28, 2023
6d4b7bf
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jul 3, 2023
e267cc2
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jul 3, 2023
406c4aa
Re-adding exception type to ``Try`` class.
staudtMarius Jul 3, 2023
eb8b06a
Merge remote-tracking branch 'origin/ms/#707-Refactor-data-sources-to…
staudtMarius Jul 3, 2023
54698a4
Fixing failing test.
staudtMarius Jul 4, 2023
d3e5435
Fixing ``sonatype-lift``.
staudtMarius Jul 4, 2023
f5fc551
Fixing ``Sonarqube`` issues.
staudtMarius Jul 4, 2023
60d030c
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jul 5, 2023
b31eb73
Fixing ``Codacy`` issues.
staudtMarius Jul 5, 2023
dd6956e
fmt
staudtMarius Jul 5, 2023
2e73ce8
Make ``Success`` and ``Failure`` final.
staudtMarius Jul 5, 2023
8200c18
Improving ``Try`` class.
staudtMarius Jul 7, 2023
0bb8333
Adding some comments.
staudtMarius Jul 7, 2023
46d4910
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
staudtMarius Jul 19, 2023
f8fd2a8
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
sebastian-peter Jul 20, 2023
8fb66d4
Merge branch 'dev' into ms/#707-Refactor-data-sources-to-throw-except…
sebastian-peter Jul 25, 2023
3f5b0c6
Implementing requested changes.
staudtMarius Jul 25, 2023
c93f93b
Fixing ``sonatype-lift`` issue.
staudtMarius Jul 25, 2023
383b59a
Fixing sonatype issue
sebastian-peter Jul 26, 2023
6414f48
Adding another test case for Try
sebastian-peter Jul 26, 2023
57bb71a
Fixing codacy issue
sebastian-peter Jul 26, 2023
68375d2
Extending test coverage of Try
sebastian-peter Jul 26, 2023
9fbe91a
Better fix for sonatype issue?
sebastian-peter Jul 26, 2023
14e458a
.
sebastian-peter Jul 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Refactor CSV data sources [#716](https://github.com/ie3-institute/PowerSystemDataModel/issues/716)
- Deleted parameter initFiles, set parameter append to false by default [#791](https://github.com/ie3-institute/PowerSystemDataModel/issues/791)
- Use nio paths instead of strings for file path [#723](https://github.com/ie3-institute/PowerSystemDataModel/issues/723)
- Data source will throw an exceptions instead of returning an empty optionals [#707](https://github.com/ie3-institute/PowerSystemDataModel/issues/707)


## [3.0.0] - 2023-02-16
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/edu/ie3/datamodel/exceptions/FailureException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.exceptions;

public class FailureException extends Exception {
public FailureException(String message, Throwable throwable) {
super(message, throwable);
}

public FailureException(String message) {
super(message);
}

public FailureException(Throwable throwable) {
super(throwable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.exceptions;

import java.util.List;

public class GraphicSourceException extends SourceException {
public GraphicSourceException(String message, List<SourceException> exceptions) {
super(message, exceptions);
}
}
14 changes: 14 additions & 0 deletions src/main/java/edu/ie3/datamodel/exceptions/RawGridException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.exceptions;

import java.util.List;

public class RawGridException extends SourceException {
public RawGridException(String message, List<SourceException> exceptions) {
super(message, exceptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
package edu.ie3.datamodel.exceptions;

import edu.ie3.datamodel.utils.ExceptionUtils;
import java.util.List;

/**
* Exception that should be used whenever an error occurs in a instance of a {@link
* edu.ie3.datamodel.io.source.DataSource}
Expand All @@ -27,4 +30,8 @@ public SourceException(final Throwable cause) {
public SourceException(final String message) {
super(message);
}

public SourceException(String message, List<? extends Exception> exceptions) {
super(message + " " + ExceptionUtils.getMessages(exceptions), exceptions.get(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.exceptions;

import java.util.List;

public class SystemParticipantsException extends SourceException {
public SystemParticipantsException(String message, List<SourceException> exceptions) {
super(message, exceptions);
}
}
12 changes: 12 additions & 0 deletions src/main/java/edu/ie3/datamodel/exceptions/TryException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.exceptions;

public class TryException extends RuntimeException {
public TryException(String message, Throwable throwable) {
super(message, throwable);
}
}
37 changes: 26 additions & 11 deletions src/main/java/edu/ie3/datamodel/io/factory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package edu.ie3.datamodel.io.factory;

import edu.ie3.datamodel.exceptions.FactoryException;
import edu.ie3.datamodel.utils.Try;
import edu.ie3.datamodel.utils.Try.*;
import java.util.*;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -39,27 +41,40 @@ public List<Class<? extends C>> getSupportedClasses() {
* data
*
* @param data EntityData (or subclass) containing the data
* @return An entity wrapped in Option if successful, an empty option otherwise
* @return An entity wrapped in a {@link Success} if successful, or an exception wrapped in a
* {@link Failure}
*/
public Optional<R> get(D data) {
public Try<R, FactoryException> get(D data) {
isSupportedClass(data.getTargetClass());

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

validateParameters(data, allFields.toArray((IntFunction<Set<String>[]>) Set[]::new));

try {
validateParameters(data, allFields.toArray((IntFunction<Set<String>[]>) Set[]::new));

// build the model
return Optional.of(buildModel(data));
return new Success<>(buildModel(data));
} catch (FactoryException e) {
// only catch FactoryExceptions, as more serious exceptions should be handled elsewhere
log.error(
"An error occurred when creating instance of {}.class.",
data.getTargetClass().getSimpleName(),
e);
return new Failure<>(
new FactoryException(
"An error occurred when creating instance of "
+ data.getTargetClass().getSimpleName()
+ ".class.",
e));
}
return Optional.empty();
}

/**
* Builds entity with data from given EntityData object after doing all kinds of checks on the
* data
*
* @param data EntityData (or subclass) containing the data wrapped in a {@link Try}
* @return An entity wrapped in a {@link Success} if successful, or an exception wrapped in a
* {@link Failure}
*/
public Try<R, FactoryException> get(Try<D, ?> data) {
return data.transformF(FactoryException::new).flatMap(this::get);
}

/**
Expand Down
Loading