summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnitsGUI
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2024-08-22 11:41:04 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2024-08-22 11:45:37 -0500
commitea3e2bf07939926e43c7abe3fd13a7c4e93f69d1 (patch)
tree958c97f05c2830aefef45f6882ba688694480b55 /src/main/java/sevenUnitsGUI
parent43e3b70003fdda6f2ccdd2471e3ee3f97ad50ada (diff)
Show unit/dim file errors as popup
Previously, any error in the unit or dimension file(s) crashes the program. Instead, 7Units now ignores any invalid lines, still parsing the correct ones, and shows a popup in case any errors happen.
Diffstat (limited to 'src/main/java/sevenUnitsGUI')
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java
index 68f0fcb..4ff2d65 100644
--- a/src/main/java/sevenUnitsGUI/Presenter.java
+++ b/src/main/java/sevenUnitsGUI/Presenter.java
@@ -40,6 +40,7 @@ import sevenUnits.unit.BaseUnit;
import sevenUnits.unit.BritishImperial;
import sevenUnits.unit.LinearUnit;
import sevenUnits.unit.LinearUnitValue;
+import sevenUnits.unit.LoadingException;
import sevenUnits.unit.Metric;
import sevenUnits.unit.Unit;
import sevenUnits.unit.UnitDatabase;
@@ -311,7 +312,7 @@ public final class Presenter {
// load units and prefixes
try (final InputStream units = inputStream(DEFAULT_UNITS_FILEPATH)) {
- this.database.loadUnitsFromStream(units);
+ this.handleLoadErrors(this.database.loadUnitsFromStream(units));
} catch (final IOException e) {
throw new AssertionError("Loading of unitsfile.txt failed.", e);
}
@@ -319,7 +320,8 @@ public final class Presenter {
// load dimensions
try (final InputStream dimensions = inputStream(
DEFAULT_DIMENSIONS_FILEPATH)) {
- this.database.loadDimensionsFromStream(dimensions);
+ this.handleLoadErrors(
+ this.database.loadDimensionsFromStream(dimensions));
} catch (final IOException e) {
throw new AssertionError("Loading of dimensionfile.txt failed.", e);
}
@@ -772,6 +774,24 @@ public final class Presenter {
}
/**
+ * Accepts a list of errors. If that list is non-empty, prints an error
+ * message and alerts the user.
+ *
+ * @since 2024-08-22
+ */
+ private void handleLoadErrors(List<LoadingException> errors) {
+ if (!errors.isEmpty()) {
+ final String errorMessage = String.format(
+ "%d error(s) happened while loading file:\n%s\n", errors.size(),
+ errors.stream().map(Throwable::getMessage)
+ .collect(Collectors.joining("\n")));
+ System.err.print(errorMessage);
+ this.view.showErrorMessage(errors.size() + "Loading Error(s)",
+ errorMessage);
+ }
+ }
+
+ /**
* @return whether or not the provided unit is semi-metric (i.e. an
* exception)
* @since 2022-04-16
@@ -832,13 +852,15 @@ public final class Presenter {
// set manually to avoid the unnecessary saving of the non-manual
// methods
case "custom_dimension_file":
- this.database.loadDimensionFile(pathFromConfig(value));
+ this.handleLoadErrors(
+ this.database.loadDimensionFile(pathFromConfig(value)));
break;
case "custom_exception_file":
this.loadExceptionFile(pathFromConfig(value));
break;
case "custom_unit_file":
- this.database.loadUnitsFile(pathFromConfig(value));
+ this.handleLoadErrors(
+ this.database.loadUnitsFile(pathFromConfig(value)));
break;
case "number_display_rule":
this.setDisplayRuleFromString(value);