Skip to content

Commit fe67dfd

Browse files
authored
More detailed error messages for Module::change_input and Module::change_submod (#379)
1 parent 63eda7c commit fe67dfd

5 files changed

Lines changed: 37 additions & 4 deletions

File tree

include/pluginplay/module/module_class.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,14 @@ bool Module::ready() const {
747747
template<typename T>
748748
void Module::change_input(const type::key& key, T&& value) {
749749
assert_not_locked_();
750+
if(!inputs().count(key)) {
751+
std::string msg = " does not have input \"" + key + "\"";
752+
if(has_name())
753+
msg = "Module \"" + get_name() + "\"" + msg;
754+
else
755+
msg = "Unnamed module" + msg;
756+
throw std::out_of_range(msg);
757+
}
750758
get_input_(key).change(std::forward<T>(value));
751759
}
752760

include/pluginplay/module_manager/module_manager_class.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ class ModuleManager {
100100
*/
101101
type::size size() const noexcept;
102102

103+
/** @brief Used to add a new module to the list
104+
*
105+
* @tparam ModuleType The type of the module being added. This type must be
106+
* constructible with no arguments and must inherit from
107+
* ModuleBase.
108+
* @param module_key
109+
*/
103110
template<typename ModuleType>
104111
void add_module(type::key module_key);
105112

@@ -108,7 +115,6 @@ class ModuleManager {
108115
* @param module_key
109116
* @param base
110117
*/
111-
112118
void add_module(type::key module_key, module_base_ptr base);
113119

114120
///@{

src/pluginplay/module/module.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ void Module::lock() { m_pimpl_->lock(); }
7878

7979
void Module::change_submod(type::key key, std::shared_ptr<Module> new_module) {
8080
assert_not_locked_();
81+
if(!submods().count(key)) {
82+
std::string msg = " does not have submodule \"" + key + "\"";
83+
if(has_name())
84+
msg = "Module \"" + get_name() + "\"" + msg;
85+
else
86+
msg = "Unnamed module" + msg;
87+
throw std::out_of_range(msg);
88+
}
8189
m_pimpl_->submods().at(key).change(new_module);
8290
}
8391

tests/cxx/unit_tests/pluginplay/catch.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
#include <catch2/catch_approx.hpp>
1919
#include <catch2/catch_template_test_macros.hpp>
2020
#include <catch2/catch_test_macros.hpp>
21+
#include <catch2/matchers/catch_matchers_exception.hpp>

tests/cxx/unit_tests/pluginplay/module/module_class.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,12 @@ TEST_CASE("Module : change_input") {
290290
}
291291
SECTION("Throws if input does not exist") {
292292
auto mod = make_module<NotReadyModule>();
293-
REQUIRE_THROWS_AS(mod->change_input("Not a key", 3), std::out_of_range);
293+
REQUIRE_THROWS_WITH(mod->change_input("Not a key", 3),
294+
"Unnamed module does not have input \"Not a key\"");
295+
mod->set_name("Test");
296+
REQUIRE_THROWS_WITH(
297+
mod->change_input("Not a key", 3),
298+
"Module \"Test\" does not have input \"Not a key\"");
294299
}
295300
SECTION("Throws if value is wrong type") {
296301
auto mod = make_module<NotReadyModule>();
@@ -316,8 +321,13 @@ TEST_CASE("Module : change_submod") {
316321
REQUIRE_THROWS_AS(p.change_submod(key, value), std::runtime_error);
317322
}
318323
SECTION("Throws if request does not exist") {
319-
REQUIRE_THROWS_AS(mod->change_submod("Not a key", value),
320-
std::out_of_range);
324+
REQUIRE_THROWS_WITH(
325+
mod->change_submod("Not a key", value),
326+
"Unnamed module does not have submodule \"Not a key\"");
327+
mod->set_name("Test");
328+
REQUIRE_THROWS_WITH(
329+
mod->change_submod("Not a key", value),
330+
"Module \"Test\" does not have submodule \"Not a key\"");
321331
}
322332
SECTION("Throws if value is wrong type") {
323333
auto mod2 = make_module<NotReadyModule>();

0 commit comments

Comments
 (0)