[systemd-devel] sdbus errors and their underlaying int value: unique?
Carlo Wood
carlo at alinoe.com
Tue Mar 2 09:40:25 UTC 2021
Hello,
thank you for you previous help; I made a lot of progress.
I'm not writing my own C++ wrappers around sbus.
I have the following question:
A sd_bus_error has a name (and a message). The name is usually something
like "org.freedesktop.DBus.Error.InvalidArgs" or
"com.alinoe.DBus.Error.test_error" - in other words it contains a
domain, "org.freedesktop.DBus.Error" and "com.alinoe.DBus.Error"
respectively.
In C++ we have std::error_code which stores both a (unique) domain
and an int that is defined within that domain. The integer values
do not have to be globally unique.
For example, org.freedesktop.DBus.Error.InvalidArgs is mapped to
EINVAL which is 22. But can I use 22 too for
com.alinoe.DBus.Error.test_error? This would be fine with
std::error_code; and I'm trying to support conversion from
DBus errors to std::error_code.
For example,
I have a class dbus::Error that simply wraps a sd_bus_error struct.
I can do:
dbus::Error error = function_returning_an_error();
std::error_code code = error; // Convert sd_bus_error to std::error_code.
std::cout << code << " [" << code.message() << "]" << std::endl;
(Can also do std::cout << error, of course, but this is for demonstration)
which could print something like:
com.alinoe.dbus.Error:22 [test_error]
or
org.freedesktop.DBus.Error:22 [Invalid argument]
Adding "com.alinoe.DBus.Error.test_error" to sdbus must be done
as follows:
enum alinoe_dbus_errors {
test_error = 22 // Is this value allowed?
};
const sd_bus_error_map alinoe_com_errors[] = {
SD_BUS_ERROR_MAP("com.alinoe.dbus.Error.test_error", test_error),
SD_BUS_ERROR_MAP_END
};
sd_bus_error_add_map(alinoe_com_errors);
I don't think I can reasonably know which codes (int values) are already
in use: many libraries could add their own errors and I don't know what
that is, or what will be added in the future (ie, to sdbus).
This is why std::error_code doesn't demand the numeric values to be
unique except inside their own domain.
Does sdbus require me to use globally unique integer values?
More information about the systemd-devel
mailing list