From ea3e2bf07939926e43c7abe3fd13a7c4e93f69d1 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Thu, 22 Aug 2024 11:41:04 -0500 Subject: 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. --- src/main/java/sevenUnitsGUI/Presenter.java | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src/main/java/sevenUnitsGUI') 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); } @@ -771,6 +773,24 @@ public final class Presenter { return this.view; } + /** + * 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 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) @@ -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); -- cgit v1.2.3