From 7da5a7ae868e37d5583693d25566883766affb32 Mon Sep 17 00:00:00 2001
From: Johannes Mey <johannes.mey@tu-dresden.de>
Date: Sun, 25 Sep 2022 21:11:57 +0200
Subject: [PATCH] throw exceptions instead of exiting on errors

---
 .../de/tudresden/inf/st/pnml/PnmlParser.java  | 24 ++++++-------------
 .../de/tudresden/inf/st/pnml/PnmlTest.java    |  3 ++-
 2 files changed, 9 insertions(+), 18 deletions(-)

diff --git a/src/main/java/de/tudresden/inf/st/pnml/PnmlParser.java b/src/main/java/de/tudresden/inf/st/pnml/PnmlParser.java
index 3c090c0..1345498 100644
--- a/src/main/java/de/tudresden/inf/st/pnml/PnmlParser.java
+++ b/src/main/java/de/tudresden/inf/st/pnml/PnmlParser.java
@@ -37,22 +37,18 @@ public class PnmlParser {
    * @param fileName the file name of a *.pnml file
    * @return a list of Petri nets
    */
-  public static List<PetriNet> parsePnml(String fileName) {
+  public static List<PetriNet> parsePnml(String fileName) throws PnmlParseException {
     Path file = Paths.get(fileName);
 
     HLAPIRootClass document = null;
 
     try {
       document = PNMLUtils.importPnmlDocument(file.toFile(), false);
-
-      logger.info(document.toPNML());
+      logger.debug(document.toPNML());
     } catch (ImportException | InvalidIDException e) {
-      logger.error("Unable to import PNML document from file '{}'", fileName);
-      logger.error("Exception was thrown!", e);
-      System.exit(-1);
+      throw new PnmlParseException("Unable to import PNML document from file '" + fileName + "'", e);
     }
 
-
     logger.info("Imported document workspace ID: {}", ModelRepository.getInstance().getCurrentDocWSId());
 
     List<PetriNet> petriNets = new ArrayList<>();
@@ -60,18 +56,12 @@ public class PnmlParser {
     fr.lip6.move.pnml.framework.general.PNType type = PNMLUtils.determinePNType(document);
     switch (type) {
       case PTNET:
-        PetriNetDocHLAPI ptDoc =
-            (PetriNetDocHLAPI) document;
-
+        PetriNetDocHLAPI ptDoc = (PetriNetDocHLAPI) document;
 
         for (fr.lip6.move.pnml.ptnet.PetriNet pmnlNet : ptDoc.getNets()) {
           PnmlParser parser;
-          try {
-            parser = new PnmlParser(pmnlNet);
-            petriNets.add(parser.getPetriNet());
-          } catch (PnmlParseException e) {
-            logger.error("Parsing the Petri net using the PNML framework failed.", e);
-          }
+          parser = new PnmlParser(pmnlNet);
+          petriNets.add(parser.getPetriNet());
         }
 
         break;
@@ -80,7 +70,7 @@ public class PnmlParser {
       case HLPN:
       case PTHLPN:
       default:
-        logger.error("Petri net is of unsupported type {}.", type.getLiteral());
+        throw new PnmlParseException("Petri net is of unsupported type " + type.getLiteral() + ".");
     }
     return petriNets;
   }
diff --git a/src/test/java/de/tudresden/inf/st/pnml/PnmlTest.java b/src/test/java/de/tudresden/inf/st/pnml/PnmlTest.java
index d94e599..4c058a0 100644
--- a/src/test/java/de/tudresden/inf/st/pnml/PnmlTest.java
+++ b/src/test/java/de/tudresden/inf/st/pnml/PnmlTest.java
@@ -2,6 +2,7 @@ package de.tudresden.inf.st.pnml;
 
 import de.tudresden.inf.st.pnml.jastadd.model.PNType;
 import de.tudresden.inf.st.pnml.jastadd.model.PetriNet;
+import de.tudresden.inf.st.pnml.jastadd.model.PnmlParseException;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ValueSource;
 import org.slf4j.Logger;
@@ -17,7 +18,7 @@ class PnmlTest {
 
   @ParameterizedTest
   @ValueSource(strings = {"src/test/resources/minimal.pnml", "src/test/resources/philo.pnml", "src/test/resources/haddadin_automaton_flat.pnml"})
-  void parsePnml(String fileName) {
+  void parsePnml(String fileName) throws PnmlParseException {
     List<PetriNet> result = PnmlParser.parsePnml(fileName);
     assertThat(result).asList()
         .isNotEmpty()
-- 
GitLab