diff --git a/co_sim_io/impl/macros.hpp b/co_sim_io/impl/macros.hpp index e2ec81e2..63eaeec5 100644 --- a/co_sim_io/impl/macros.hpp +++ b/co_sim_io/impl/macros.hpp @@ -21,11 +21,59 @@ the code where the CoSimIO is included #ifndef CO_SIM_IO_ERROR #include + #include #include - struct err { // helper struct to mimic behavior of KRATOS_ERROR - ~err() noexcept(false) { throw std::runtime_error("Error: "); } // destructors are noexcept by default + #include + + namespace CoSimIO { + + // Simplified version of kratos/includes/exception.h + class Exception : public std::exception + { + public: + explicit Exception(const std::string& rWhat) : std::exception(), mMessage(rWhat) { } + + const char* what() const noexcept override + { + return mMessage.c_str(); + } + + /// string stream function + template + Exception& operator << (StreamValueType const& rValue) + { + std::stringstream buffer; + buffer << rValue; + + mMessage.append(buffer.str()); + + return *this; + } + + Exception& operator << (std::ostream& (*pf)(std::ostream&)) + { + std::stringstream buffer; + pf(buffer); + + mMessage.append(buffer.str()); + + return *this; + } + + Exception& operator << (const char* pString) + { + mMessage.append(pString); + return *this; + } + + private: + std::string mMessage; + }; - #define CO_SIM_IO_ERROR (err(), std::cout) + + } // namespace CoSimIO + + #define CO_SIM_IO_ERROR throw CoSimIO::Exception("Error: ") #endif #ifndef CO_SIM_IO_ERROR_IF @@ -45,4 +93,4 @@ the code where the CoSimIO is included #define CO_SIM_IO_INFO_IF(label, conditional) if (conditional) CO_SIM_IO_INFO(label) #endif -#endif // CO_SIM_IO_MACROS_INCLUDED \ No newline at end of file +#endif // CO_SIM_IO_MACROS_INCLUDED diff --git a/tests/co_sim_io/impl/test_info.cpp b/tests/co_sim_io/impl/test_info.cpp index fbc49a37..781b2bb1 100644 --- a/tests/co_sim_io/impl/test_info.cpp +++ b/tests/co_sim_io/impl/test_info.cpp @@ -140,7 +140,7 @@ TEST_CASE("info_non_existing_key") CHECK_UNARY_FALSE(info.Has("identifier")); - CHECK_THROWS_WITH(info.Get("identifier"), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(info.Get("identifier"), "Error: Trying to get \"identifier\" which does not exist!\n"); } TEST_CASE("info_wrong_type") @@ -152,7 +152,7 @@ TEST_CASE("info_wrong_type") info.Set("identifier", "pressure"); CHECK_UNARY(info.Has("identifier")); - CHECK_THROWS_WITH(info.Get("identifier"), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(info.Get("identifier"), "Error: Wrong DataType! Trying to get \"identifier\" which is of type \"string\" with \"int\"!\n"); } TEST_CASE("info_many_values") diff --git a/tests/co_sim_io/impl/test_model_part.cpp b/tests/co_sim_io/impl/test_model_part.cpp index 47d42a04..c63c7cf4 100644 --- a/tests/co_sim_io/impl/test_model_part.cpp +++ b/tests/co_sim_io/impl/test_model_part.cpp @@ -58,12 +58,12 @@ TEST_CASE("node_negative_id") SUBCASE("from_coords") { - CHECK_THROWS_WITH(Node(id, coords[0], coords[1], coords[2]), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(Node(id, coords[0], coords[1], coords[2]), "Error: Id must be >= 1!\n"); } SUBCASE("from_coords_array") { - CHECK_THROWS_WITH(Node(id, coords), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(Node(id, coords), "Error: Id must be >= 1!\n"); } } @@ -102,12 +102,12 @@ TEST_CASE("element_checks") SUBCASE("negative_id") { - CHECK_THROWS_WITH(Element element(id, type, {&node}), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(Element element(id, type, {&node}), "Error: Id must be >= 1!\n"); } SUBCASE("no_nodes") { - CHECK_THROWS_WITH(Element element(1, type, {}), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(Element element(1, type, {}), "Error: No nodes were passed!\n"); } } @@ -166,8 +166,8 @@ TEST_CASE("model_part_basics") TEST_CASE("model_part_invalid_names") { - CHECK_THROWS_WITH(ModelPart model_part(""), "Error: "); // TODO find a better way of testing this - CHECK_THROWS_WITH(ModelPart model_part("my_name.ssss"), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(ModelPart model_part(""), "Error: Please don't use empty names (\"\") when creating a ModelPart\n"); + CHECK_THROWS_WITH(ModelPart model_part("my_name.ssss"), "Error: Please don't use names containing (\".\") when creating a ModelPart (used in \"my_name.ssss\")\n"); } TEST_CASE("model_part_create_new_node") @@ -213,7 +213,7 @@ TEST_CASE("model_part_create_new_node_twice") model_part.CreateNewNode(1, 0,0,0); REQUIRE_EQ(model_part.NumberOfNodes(), 1); - CHECK_THROWS_WITH(model_part.CreateNewNode(1, 0,0,0), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(model_part.CreateNewNode(1, 0,0,0), "Error: The Node with Id 1 exists already!\n"); } TEST_CASE("model_part_get_node") @@ -240,7 +240,7 @@ TEST_CASE("model_part_get_node") SUBCASE("non_existing") { - CHECK_THROWS_WITH(model_part.GetNode(node_id+1), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(model_part.GetNode(node_id+1), "Error: Node with Id 692 does not exist!\n"); } } @@ -310,7 +310,7 @@ TEST_CASE("model_part_create_new_element_twice") model_part.CreateNewElement(1, 5, {1}); REQUIRE_EQ(model_part.NumberOfElements(), 1); - CHECK_THROWS_WITH(model_part.CreateNewElement(1, 5, {1}), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(model_part.CreateNewElement(1, 5, {1}), "Error: The Element with Id 1 exists already!\n"); } TEST_CASE("model_part_get_element") @@ -334,7 +334,7 @@ TEST_CASE("model_part_get_element") SUBCASE("non_existing") { - CHECK_THROWS_WITH(model_part.GetElement(elem_id+1), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(model_part.GetElement(elem_id+1), "Error: Element with Id 7 does not exist!\n"); } } diff --git a/tests/co_sim_io/python/test_info.py b/tests/co_sim_io/python/test_info.py index d280f8e1..33e81dce 100644 --- a/tests/co_sim_io/python/test_info.py +++ b/tests/co_sim_io/python/test_info.py @@ -107,7 +107,7 @@ def test_non_existing_key(self): self.assertFalse(info.Has("identifier")) - with self.assertRaisesRegex(Exception, 'Error: '): # TODO improve this once errors are working better + with self.assertRaisesRegex(Exception, 'Error: Trying to get "identifier" which does not exist!'): info.GetBool("identifier") def test_wrong_type(self): @@ -119,7 +119,7 @@ def test_wrong_type(self): self.assertTrue(info.Has("identifier")) - with self.assertRaisesRegex(Exception, 'Error: '): # TODO improve this once errors are working better + with self.assertRaisesRegex(Exception, 'Error: Wrong DataType! Trying to get "identifier" which is of type "string" with "bool"!'): info.GetBool("identifier") def test_many_values(self):