From 3e8e5eed03267f948a3df35bf9bb65e8c90330d1 Mon Sep 17 00:00:00 2001
From: Paul Nitzke <14367-paulenit@users.noreply.git.rwth-aachen.de>
Date: Mon, 20 Jun 2022 01:52:49 +0200
Subject: [PATCH] Add makeshift error handling

* Make thrown exceptions more expressive
* Catch exceptions in Main method
---
 src/SparseTransform.CLI/Program.cs            | 49 ++++++++++++-------
 src/SparseTransform/Convert/ErrorListener.cs  |  2 +-
 .../Convert/MatrixMarketReader.cs             | 31 +++++-------
 3 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/src/SparseTransform.CLI/Program.cs b/src/SparseTransform.CLI/Program.cs
index 952047c..416dbe6 100644
--- a/src/SparseTransform.CLI/Program.cs
+++ b/src/SparseTransform.CLI/Program.cs
@@ -10,31 +10,46 @@ public class Program
         Parser.Default.ParseArguments<ConvertOptions, PartitionOptions>(args)
             .WithParsed<ConvertOptions>(o =>
             {
-
-                String MMMatrix = File.ReadAllText(o.Input);
-                String DotString = Convert2Dot(MMMatrix);
-                File.WriteAllText(o.Output, DotString);
+                try
+                {
+                    String MMMatrix = File.ReadAllText(o.Input);
+                    String DotString = Convert2Dot(MMMatrix);
+                    File.WriteAllText(o.Output, DotString);
+                }
+                catch (Exception e)
+                {
+                    Console.Out.WriteLine(e.Message);
+                    return;
+                }
 
             })
             .WithParsed<PartitionOptions>(o =>
             {
-                String MMMatrix = File.ReadAllText(o.Input);
-                String Output = "";
-
-                if (o.Format == Formats.ColoredDot)
+                try
                 {
-                    Output = Partition2ColoredDot(MMMatrix, o.TextColor);
-                }
-                else if (o.Format == Formats.SeedMatrix)
-                {
-                    Output = Partition2SeedMatrix(MMMatrix);
+                    String MMMatrix = File.ReadAllText(o.Input);
+                    String Output = "";
+
+                    if (o.Format == Formats.ColoredDot)
+                    {
+                        Output = Partition2ColoredDot(MMMatrix, o.TextColor);
+                    }
+                    else if (o.Format == Formats.SeedMatrix)
+                    {
+                        Output = Partition2SeedMatrix(MMMatrix);
+                    }
+                    else if (o.Format == Formats.CompressedMatrix)
+                    {
+                        Output = Partition2CompressedMatrix(MMMatrix);
+                    }
+
+                    File.WriteAllText(o.Output, Output);
                 }
-                else if (o.Format == Formats.CompressedMatrix)
+                catch (Exception e)
                 {
-                    Output = Partition2CompressedMatrix(MMMatrix);
+                    Console.Out.WriteLine(e.Message);
+                    return;
                 }
-
-                File.WriteAllText(o.Output, Output);
             });
     }
 
diff --git a/src/SparseTransform/Convert/ErrorListener.cs b/src/SparseTransform/Convert/ErrorListener.cs
index 657aacc..4fd6daf 100644
--- a/src/SparseTransform/Convert/ErrorListener.cs
+++ b/src/SparseTransform/Convert/ErrorListener.cs
@@ -22,7 +22,7 @@ namespace Transform.Convert
         /// <exception cref="Exception"></exception>
         public void SyntaxError([NotNull] IRecognizer recognizer, [Nullable] IToken offendingSymbol, int line, int charPositionInLine, [NotNull] string msg, [Nullable] RecognitionException e)
         {
-            throw new Exception(msg);
+            throw new Exception("Error in MatrixMarket header line: " + msg);
         }
     }
 }
diff --git a/src/SparseTransform/Convert/MatrixMarketReader.cs b/src/SparseTransform/Convert/MatrixMarketReader.cs
index 27a114d..ee16ecc 100644
--- a/src/SparseTransform/Convert/MatrixMarketReader.cs
+++ b/src/SparseTransform/Convert/MatrixMarketReader.cs
@@ -86,7 +86,7 @@ namespace SparseTransform.Convert
        /// checks whether the comment-section is valid.
        /// </summary>
        /// <param name="lines">entire input split by newlines</param>
-       /// <exception cref="Exception"></exception>
+       /// <exception cref="FormatException"></exception>
         private void checkComments(string[] lines)
         {
             bool leftComments = false;
@@ -96,7 +96,7 @@ namespace SparseTransform.Convert
                 {
                     if (leftComments && line.StartsWith('%'))
                     {
-                        throw new Exception("comment section mismatch on input");
+                        throw new FormatException("Syntax Error in MatrixMarket file: Encountered more comments in between data lines.");
                     }
                     else
                     {
@@ -126,31 +126,24 @@ namespace SparseTransform.Convert
             string[] values;
             int lineCount = getFirstDataLine(matrix);
             matrix[lineCount] = "% " + matrix[lineCount];
-            try
+            foreach (String line in matrix)
             {
-                foreach (String line in matrix)
+                if (line != "")
                 {
-                    if (line != "")
+                    if (!line.StartsWith('%'))
                     {
-                        if (!line.StartsWith('%'))
-                        {
 
-                            values = line.Split(" ");
-                            if (values.Length != 3)
-                            {
-                                throw new Exception();
-                            }
-                            int xCoordinate = Int32.Parse(values[0]);
-                            int yCoordinate = Int32.Parse(values[1]);
-                            processor(xCoordinate, yCoordinate, Double.Parse(values[2]));
+                        values = line.Split(" ");
+                        if (values.Length != 3)
+                        {
+                            throw new ArgumentException("Syntax Error in MatrixMarket file: Data line did not contain three elements.");
                         }
+                        int xCoordinate = Int32.Parse(values[0]);
+                        int yCoordinate = Int32.Parse(values[1]);
+                        processor(xCoordinate, yCoordinate, Double.Parse(values[2]));
                     }
                 }
             }
-            catch (Exception e)
-            {
-                throw e;
-            }
         }
 
 
-- 
GitLab