internal/utils: Errors now have names
diff --git a/src/sdeventplus/internal/utils.hpp b/src/sdeventplus/internal/utils.hpp
index c0b0e68..17e8cee 100644
--- a/src/sdeventplus/internal/utils.hpp
+++ b/src/sdeventplus/internal/utils.hpp
@@ -19,7 +19,7 @@
// Helpers for sd_event callbacks to handle exceptions gracefully
template <typename Func, typename... Args>
-static int performCallback(Func func, Args... args)
+static int performCallback(const char* name, Func func, Args... args)
{
try
{
@@ -28,17 +28,17 @@
}
catch (const std::system_error& e)
{
- fprintf(stderr, "sdeventplus: callback: %s\n", e.what());
+ fprintf(stderr, "sdeventplus: %s: %s\n", name, e.what());
return -e.code().value();
}
catch (const std::exception& e)
{
- fprintf(stderr, "sdeventplus: callback: %s\n", e.what());
+ fprintf(stderr, "sdeventplus: %s: %s\n", name, e.what());
return -ENOSYS;
}
catch (...)
{
- fprintf(stderr, "sdeventplus: callback: Unknown error\n");
+ fprintf(stderr, "sdeventplus: %s: Unknown error\n", name);
return -ENOSYS;
}
}
diff --git a/src/sdeventplus/source/base.cpp b/src/sdeventplus/source/base.cpp
index 5f202db..32e6edf 100644
--- a/src/sdeventplus/source/base.cpp
+++ b/src/sdeventplus/source/base.cpp
@@ -61,7 +61,8 @@
return -EINVAL;
}
Base* base = reinterpret_cast<Base*>(userdata);
- return internal::performCallback(base->get_prepare(), std::ref(*base));
+ return internal::performCallback("prepare_callback", base->get_prepare(),
+ std::ref(*base));
}
void Base::set_prepare(Callback&& callback)
diff --git a/test/internal/utils.cpp b/test/internal/utils.cpp
index 2a347ca..403964c 100644
--- a/test/internal/utils.cpp
+++ b/test/internal/utils.cpp
@@ -12,25 +12,27 @@
TEST(UtilsTest, PerformCallbackSuccess)
{
- EXPECT_EQ(0, performCallback([]() {}));
+ EXPECT_EQ(0, performCallback(nullptr, []() {}));
}
TEST(UtilsTest, SetPrepareSystemError)
{
- EXPECT_EQ(-EBUSY, performCallback([]() {
+ EXPECT_EQ(-EBUSY, performCallback("system_error", []() {
throw std::system_error(EBUSY, std::generic_category());
}));
}
TEST(UtilsTest, SetPrepareException)
{
- EXPECT_EQ(-ENOSYS,
- performCallback([]() { throw std::runtime_error("Exception"); }));
+ EXPECT_EQ(-ENOSYS, performCallback("runtime_error", []() {
+ throw std::runtime_error("Exception");
+ }));
}
TEST(UtilsTest, SetPrepareUnknownException)
{
- EXPECT_EQ(-ENOSYS, performCallback([]() { throw static_cast<int>(1); }));
+ EXPECT_EQ(-ENOSYS,
+ performCallback("unknown", []() { throw static_cast<int>(1); }));
}
} // namespace