refactor: improve handling of exceptions from callback handlers (#375)

* Catch and process all exceptions (not just sdbus::Error) from callback handlers
* Unify handling of exceptions from all types of callbacks -- always set sd_bus_error and return a negative result number in case of exception

Although libsystemd logs (with DEBUG severity) all errors from such callback handlers (except method callback handler), it seems to be out of our control. One of handy sdbus-c++ features could be the ability for clients to install a log callback, which sdbus-c++ would call in case of exceptions flying from callback handlers. In case something doesn't work for clients (especially novices), they can first look into these logs.

This may be handy in common situations like ignored signals on client side because of the inadvertent mismatch between real signal signature and signal handler signature. Like here: #373. (Although in this specific case of signals, there is a solution with an additional const sdbus::Error* argument that would reveal such an error.)
This commit is contained in:
Stanislav Angelovič
2023-11-03 18:03:01 +01:00
committed by GitHub
parent 9490b3351f
commit e2b3e98374
6 changed files with 51 additions and 49 deletions
+5 -4
View File
@@ -595,9 +595,8 @@ namespace sdbus {
}
catch (const Error& e)
{
// Catch message unpack exceptions and pass them to the callback
// in the expected manner to avoid propagating them up the call
// stack to the event loop.
// Pass message deserialization exceptions to the client via callback error parameter,
// instead of propagating them up the message loop call stack.
sdbus::apply(callback, &e, args);
return;
}
@@ -676,8 +675,10 @@ namespace sdbus {
}
catch (const sdbus::Error& e)
{
// Invoke callback with error argument and input arguments from the tuple.
// Pass message deserialization exceptions to the client via callback error parameter,
// instead of propagating them up the message loop call stack.
sdbus::apply(callback, &e, signalArgs);
return;
}
// Invoke callback with no error and input arguments from the tuple.
+2
View File
@@ -76,6 +76,8 @@ namespace sdbus {
};
sdbus::Error createError(int errNo, const std::string& customMsg);
inline const char* SDBUSCPP_ERROR_NAME = "org.sdbuscpp.Error";
}
#define SDBUS_THROW_ERROR(_MSG, _ERRNO) \