sdbus++: enhance exception information
Use both 'name' and 'description' for exceptions. All sdbusplus
exceptions now have a 'name()' and 'description()' method which
give the dbus error name and a human-readable description
respectively. The standard 'what' is now '<name>: <description>'.
Change-Id: Ic6cdb88350c07589cbfbf233e84a575632383af6
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/sdbusplus/exception.hpp b/sdbusplus/exception.hpp
index 01687a4..f5f6ab6 100644
--- a/sdbusplus/exception.hpp
+++ b/sdbusplus/exception.hpp
@@ -12,6 +12,8 @@
* by the bindings. */
struct exception : public std::exception
{
+ virtual const char* name() const noexcept = 0;
+ virtual const char* description() const noexcept = 0;
};
/** base exception class for all errors generated by sdbusplus itself. */
diff --git a/tools/sdbusplus/templates/error.mako.cpp b/tools/sdbusplus/templates/error.mako.cpp
index 62cc541..74cefdc 100644
--- a/tools/sdbusplus/templates/error.mako.cpp
+++ b/tools/sdbusplus/templates/error.mako.cpp
@@ -9,9 +9,17 @@
namespace Error
{
% for e in error.errors:
+const char* ${e.name}::name() const noexcept
+{
+ return errName;
+}
+const char* ${e.name}::description() const noexcept
+{
+ return errDesc;
+}
const char* ${e.name}::what() const noexcept
{
- return name;
+ return errWhat;
}
% endfor
diff --git a/tools/sdbusplus/templates/error.mako.hpp b/tools/sdbusplus/templates/error.mako.hpp
index 51f4860..2f34882 100644
--- a/tools/sdbusplus/templates/error.mako.hpp
+++ b/tools/sdbusplus/templates/error.mako.hpp
@@ -12,12 +12,16 @@
{
% for e in error.errors:
-struct ${e.name} : public sdbusplus::exception_t
+struct ${e.name} final : public sdbusplus::exception_t
{
- static constexpr auto name = "${error.name}.${e.name}";
- static constexpr auto description =
+ static constexpr auto errName = "${error.name}.${e.name}";
+ static constexpr auto errDesc =
"${e.description.strip()}";
+ static constexpr auto errWhat =
+ "${error.name}.${e.name}: ${e.description.strip()}";
+ const char* name() const noexcept override;
+ const char* description() const noexcept override;
const char* what() const noexcept override;
};
diff --git a/tools/sdbusplus/templates/method.mako.prototype.hpp b/tools/sdbusplus/templates/method.mako.prototype.hpp
index d9c1ec7..090ac55 100644
--- a/tools/sdbusplus/templates/method.mako.prototype.hpp
+++ b/tools/sdbusplus/templates/method.mako.prototype.hpp
@@ -137,15 +137,13 @@
}
catch(sdbusplus::internal_exception_t& e)
{
- auto name = e.what();
- sd_bus_error_set_const(error, name, name);
+ sd_bus_error_set_const(error, e.name(), e.description());
return -EINVAL;
}
% for e in method.errors:
catch(sdbusplus::${error_namespace(e)}::${error_name(e)}& e)
{
- auto name = e.what();
- sd_bus_error_set_const(error, name, name);
+ sd_bus_error_set_const(error, e.name(), e.description());
return -EINVAL;
}
% endfor