From 62881a71840faadb3d5cc7d0859c2f2c8bfdd323 Mon Sep 17 00:00:00 2001 From: "Jonathan M. Waldrop" Date: Fri, 15 May 2026 14:17:50 -0500 Subject: [PATCH] More detailed error messages for Module::change_input and Module::change_submod --- include/pluginplay/module/module_class.hpp | 8 ++++++++ .../module_manager/module_manager_class.hpp | 8 +++++++- src/pluginplay/module/module.cpp | 8 ++++++++ tests/cxx/unit_tests/pluginplay/catch.hpp | 1 + .../pluginplay/module/module_class.cpp | 16 +++++++++++++--- 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/include/pluginplay/module/module_class.hpp b/include/pluginplay/module/module_class.hpp index f6dc01637..37494d18b 100644 --- a/include/pluginplay/module/module_class.hpp +++ b/include/pluginplay/module/module_class.hpp @@ -747,6 +747,14 @@ bool Module::ready() const { template void Module::change_input(const type::key& key, T&& value) { assert_not_locked_(); + if(!inputs().count(key)) { + std::string msg = " does not have input \"" + key + "\""; + if(has_name()) + msg = "Module \"" + get_name() + "\"" + msg; + else + msg = "Unnamed module" + msg; + throw std::out_of_range(msg); + } get_input_(key).change(std::forward(value)); } diff --git a/include/pluginplay/module_manager/module_manager_class.hpp b/include/pluginplay/module_manager/module_manager_class.hpp index 3249514f4..7ba2befb7 100644 --- a/include/pluginplay/module_manager/module_manager_class.hpp +++ b/include/pluginplay/module_manager/module_manager_class.hpp @@ -100,6 +100,13 @@ class ModuleManager { */ type::size size() const noexcept; + /** @brief Used to add a new module to the list + * + * @tparam ModuleType The type of the module being added. This type must be + * constructible with no arguments and must inherit from + * ModuleBase. + * @param module_key + */ template void add_module(type::key module_key); @@ -108,7 +115,6 @@ class ModuleManager { * @param module_key * @param base */ - void add_module(type::key module_key, module_base_ptr base); ///@{ diff --git a/src/pluginplay/module/module.cpp b/src/pluginplay/module/module.cpp index f5090342d..574cc1711 100644 --- a/src/pluginplay/module/module.cpp +++ b/src/pluginplay/module/module.cpp @@ -78,6 +78,14 @@ void Module::lock() { m_pimpl_->lock(); } void Module::change_submod(type::key key, std::shared_ptr new_module) { assert_not_locked_(); + if(!submods().count(key)) { + std::string msg = " does not have submodule \"" + key + "\""; + if(has_name()) + msg = "Module \"" + get_name() + "\"" + msg; + else + msg = "Unnamed module" + msg; + throw std::out_of_range(msg); + } m_pimpl_->submods().at(key).change(new_module); } diff --git a/tests/cxx/unit_tests/pluginplay/catch.hpp b/tests/cxx/unit_tests/pluginplay/catch.hpp index 42e8ef612..5f95d58cd 100644 --- a/tests/cxx/unit_tests/pluginplay/catch.hpp +++ b/tests/cxx/unit_tests/pluginplay/catch.hpp @@ -18,3 +18,4 @@ #include #include #include +#include diff --git a/tests/cxx/unit_tests/pluginplay/module/module_class.cpp b/tests/cxx/unit_tests/pluginplay/module/module_class.cpp index fd0749c95..4e9e12fe4 100644 --- a/tests/cxx/unit_tests/pluginplay/module/module_class.cpp +++ b/tests/cxx/unit_tests/pluginplay/module/module_class.cpp @@ -290,7 +290,12 @@ TEST_CASE("Module : change_input") { } SECTION("Throws if input does not exist") { auto mod = make_module(); - REQUIRE_THROWS_AS(mod->change_input("Not a key", 3), std::out_of_range); + REQUIRE_THROWS_WITH(mod->change_input("Not a key", 3), + "Unnamed module does not have input \"Not a key\""); + mod->set_name("Test"); + REQUIRE_THROWS_WITH( + mod->change_input("Not a key", 3), + "Module \"Test\" does not have input \"Not a key\""); } SECTION("Throws if value is wrong type") { auto mod = make_module(); @@ -316,8 +321,13 @@ TEST_CASE("Module : change_submod") { REQUIRE_THROWS_AS(p.change_submod(key, value), std::runtime_error); } SECTION("Throws if request does not exist") { - REQUIRE_THROWS_AS(mod->change_submod("Not a key", value), - std::out_of_range); + REQUIRE_THROWS_WITH( + mod->change_submod("Not a key", value), + "Unnamed module does not have submodule \"Not a key\""); + mod->set_name("Test"); + REQUIRE_THROWS_WITH( + mod->change_submod("Not a key", value), + "Module \"Test\" does not have submodule \"Not a key\""); } SECTION("Throws if value is wrong type") { auto mod2 = make_module();