From 4430d61bfc67c09f73d67c6f9cf9a96cb2121da7 Mon Sep 17 00:00:00 2001
From: Simon Praetorius <simon.praetorius@tu-dresden.de>
Date: Fri, 18 Sep 2020 16:45:11 +0200
Subject: [PATCH] proper error messages using DUNE_THROW

---
 dune/vtk/utility/CMakeLists.txt |  1 +
 dune/vtk/utility/errors.hh      | 23 +++++++++++++++++++
 dune/vtk/vtkreader.impl.hh      | 39 +++++++++++++++++----------------
 3 files changed, 44 insertions(+), 19 deletions(-)
 create mode 100644 dune/vtk/utility/errors.hh

diff --git a/dune/vtk/utility/CMakeLists.txt b/dune/vtk/utility/CMakeLists.txt
index efed088..a611ab9 100644
--- a/dune/vtk/utility/CMakeLists.txt
+++ b/dune/vtk/utility/CMakeLists.txt
@@ -4,6 +4,7 @@ dune_add_library("filesystem" OBJECT
 #install headers
 install(FILES
   enum.hh
+  errors.hh
   filesystem.hh
   lagrangepoints.hh
   lagrangepoints.impl.hh
diff --git a/dune/vtk/utility/errors.hh b/dune/vtk/utility/errors.hh
new file mode 100644
index 0000000..2961c0c
--- /dev/null
+++ b/dune/vtk/utility/errors.hh
@@ -0,0 +1,23 @@
+#pragma once
+
+#include <dune/common/exceptions.hh>
+
+/**
+ * \file
+ * \brief Macro for wrapping error checks and throwing exceptions
+ */
+
+namespace Dune {
+
+class VtkError : public Exception {};
+
+}
+
+/**
+ * \brief check if condition \a cond holds; otherwise, throw a VtkError.
+ */
+#define VTK_ASSERT(cond, text)          \
+  do {                                  \
+    if (!(cond))                        \
+      DUNE_THROW(Dune::VtkError, text); \
+  } while (false)
diff --git a/dune/vtk/vtkreader.impl.hh b/dune/vtk/vtkreader.impl.hh
index 308b33d..b9f42f8 100644
--- a/dune/vtk/vtkreader.impl.hh
+++ b/dune/vtk/vtkreader.impl.hh
@@ -10,6 +10,7 @@
 #include <dune/common/classname.hh>
 #include <dune/common/version.hh>
 
+#include "utility/errors.hh"
 #include "utility/filesystem.hh"
 #include "utility/string.hh"
 
@@ -32,7 +33,7 @@ void VtkReader<Grid,Creator>::read (std::string const& filename, bool fillCreato
   } else if (ext == ".pvtu") {
     readParallelFileFromStream(input, comm().rank(), comm().size(), fillCreator);
   } else {
-    DUNE_THROW(IOError, "File has unknown file-extension '" << ext << "'. Allowed are only '.vtu' and '.pvtu'.");
+    DUNE_THROW(Dune::VtkError, "File has unknown file-extension '" << ext << "'. Allowed are only '.vtu' and '.pvtu'.");
   }
 }
 
@@ -56,16 +57,16 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
       auto attr = parseXml(line, closed);
 
       if (!attr["type"].empty())
-        assert(attr["type"] == "UnstructuredGrid");
+        VTK_ASSERT(attr["type"] == "UnstructuredGrid", "VtkReader supports UnstructuredGrid types");
       if (!attr["version"].empty())
-        assert(std::stod(attr["version"]) == 1.0);
+        VTK_ASSERT(std::stod(attr["version"]) == 1.0, "File format must be 1.0");
       if (!attr["byte_order"].empty())
-        assert(attr["byte_order"] == "LittleEndian");
+        VTK_ASSERT(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported");
       if (!attr["header_type"].empty())
-        assert(attr["header_type"] == "UInt64");
+        VTK_ASSERT(attr["header_type"] == "UInt64", "Header integer type must be UInt64");
       if (!attr["compressor"].empty()) {
         compressor = attr["compressor"];
-        assert(compressor == "vtkZLibDataCompressor"); // only ZLib compression supported
+        VTK_ASSERT(compressor == "vtkZLibDataCompressor", "Only ZLib compression supported");
       }
       section = VTK_FILE;
     }
@@ -79,7 +80,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
       bool closed = false;
       auto attr = parseXml(line, closed);
 
-      assert(attr.count("NumberOfPoints") > 0 && attr.count("NumberOfCells") > 0);
+      VTK_ASSERT(attr.count("NumberOfPoints") > 0 && attr.count("NumberOfCells") > 0, "Number of points or cells in file must be > 0");
       numberOfPoints_ = std::stoul(attr["NumberOfPoints"]);
       numberOfCells_ = std::stoul(attr["NumberOfCells"]);
       section = PIECE;
@@ -129,7 +130,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
       data_offset = 0;
       if (!attr["offset"].empty()) {
         data_offset = std::stoul(attr["offset"]);
-        assert(data_format == "appended");
+        VTK_ASSERT(data_format == "appended", "Attribute 'offset' only supported by appended mode");
       }
 
       // Store attributes of DataArray
@@ -156,7 +157,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
       else if (section == CELLS)
         section = CELLS_DATA_ARRAY;
       else
-        DUNE_THROW(Exception, "Wrong section for <DataArray>");
+        DUNE_THROW(Dune::VtkError, "Wrong section for <DataArray>");
     }
     else if (line.substr(1,10) == "/DataArray") {
       if (section == PD_DATA_ARRAY)
@@ -168,13 +169,13 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
       else if (section == CELLS_DATA_ARRAY)
         section = CELLS;
       else
-        DUNE_THROW(Exception, "Wrong section for </DataArray>");
+        DUNE_THROW(Dune::VtkError, "Wrong section for </DataArray>");
     }
     else if (isSection(line, "AppendedData", section, VTK_FILE)) {
       bool closed = false;
       auto attr = parseXml(line, closed);
       if (!attr["encoding"].empty())
-        assert(attr["encoding"] == "raw"); // base64 encoding not supported
+        VTK_ASSERT(attr["encoding"] == "raw", "base64 encoding not supported");
 
       offset0_ = findAppendedDataPosition(input);
       if (dataArray_["points"].type == Vtk::FLOAT32)
@@ -223,7 +224,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
   }
 
   if (section != NO_SECTION)
-    DUNE_THROW(IOError, "VTK-File is incomplete. It must end with </VTKFile>!");
+    DUNE_THROW(Dune::VtkError, "VTK-File is incomplete. It must end with </VTKFile>!");
 
   if (fillCreator)
     fillGridCreator();
@@ -244,15 +245,15 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input,
       auto attr = parseXml(line, closed);
 
       if (!attr["type"].empty())
-        assert(attr["type"] == "PUnstructuredGrid");
+        VTK_ASSERT(attr["type"] == "PUnstructuredGrid", "VtkReader supports PUnstructuredGrid types");
       if (!attr["version"].empty())
-        assert(std::stod(attr["version"]) == 1.0);
+        VTK_ASSERT(std::stod(attr["version"]) == 1.0, "File format must be 1.0");
       if (!attr["byte_order"].empty())
-        assert(attr["byte_order"] == "LittleEndian");
+        VTK_ASSERT(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported");
       if (!attr["header_type"].empty())
-        assert(attr["header_type"] == "UInt64");
+        VTK_ASSERT(attr["header_type"] == "UInt64", "Header integer type must be UInt64");
       if (!attr["compressor"].empty())
-        assert(attr["compressor"] == "vtkZLibDataCompressor"); // only ZLib compression supported
+        VTK_ASSERT(attr["compressor"] == "vtkZLibDataCompressor", "Only ZLib compression supported");
       section = VTK_FILE;
     }
     else if (isSection(line, "/VTKFile", section, VTK_FILE))
@@ -265,7 +266,7 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input,
       bool closed = false;
       auto attr = parseXml(line, closed);
 
-      assert(attr.count("Source") > 0);
+      VTK_ASSERT(attr.count("Source") > , "No source files for partitions provided");
       pieces_.push_back(attr["Source"]);
     }
 
@@ -274,7 +275,7 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input,
   }
 
   if (section != NO_SECTION)
-    DUNE_THROW(IOError, "VTK-File is incomplete. It must end with </VTKFile>!");
+    DUNE_THROW(Dune::VtkError, "VTK-File is incomplete. It must end with </VTKFile>!");
 
   if (fillCreator)
     fillGridCreator();
-- 
GitLab