Skip to content

Commit

Permalink
sdbus++: events: create json for events
Browse files Browse the repository at this point in the history
Create JSON for the events containing their metadata and leverage that
as the "message" portion of the `sd_bus_error`.  This will allow
unpacking the message on a client side back to the original exception.

Tested:
```
$ busctl --user call net.poettering.Calculator /net/poettering/calculator net.poettering.Calculator Divide xx 5 0
Call failed: {"net.poettering.Calculator.DivisionByZero":{"FOO":"unused"}}
```

Signed-off-by: Patrick Williams <[email protected]>
Change-Id: I4edd76d983267f28e51c4aa41902d45f5d6da793
  • Loading branch information
williamspatrick committed Sep 25, 2024
1 parent a4bfefd commit 14c4797
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Meson build directories.
/build*/
/subprojects/*/
/subprojects/*
!/subprojects/*.wrap

# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
Expand Down
4 changes: 4 additions & 0 deletions include/sdbusplus/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <systemd/sd-bus.h>

#include <nlohmann/json_fwd.hpp>
#include <sdbusplus/sdbus.hpp>
#include <sdbusplus/utility/consteval_string.hpp>

Expand Down Expand Up @@ -72,6 +73,9 @@ struct generated_event : public generated_exception

template <utility::details::consteval_string_holder V>
using metadata_t = utility::consteval_string<V>;

protected:
mutable std::string jsonString;
};

/** base exception class for all errors generated by sdbusplus itself. */
Expand Down
12 changes: 10 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project('sdbusplus', 'cpp', 'c',
)

libsystemd_pkg = dependency('libsystemd')
nlohmann_json_dep = dependency('nlohmann_json', include_type: 'system')

python = import('python')
python_bin = python.find_installation('python3', modules:['inflection', 'yaml', 'mako'])
Expand Down Expand Up @@ -40,7 +41,10 @@ libsdbusplus = library(
'sdbusplus',
libsdbusplus_src,
include_directories: root_inc,
dependencies: libsystemd_pkg,
dependencies: [
libsystemd_pkg,
nlohmann_json_dep
],
version: meson.project_version(),
install: true,
)
Expand All @@ -60,7 +64,11 @@ boost_dep = declare_dependency(
sdbusplus_dep = declare_dependency(
include_directories: root_inc,
link_with: libsdbusplus,
dependencies: [ libsystemd_pkg, boost_dep ],
dependencies: [
boost_dep,
libsystemd_pkg,
nlohmann_json_dep,
],
)

subdir('tools')
Expand Down
6 changes: 6 additions & 0 deletions subprojects/nlohmann_json.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[wrap-git]
revision = HEAD
url = https://github.com/nlohmann/json.git

[provide]
nlohmann_json = nlohmann_json_dep
28 changes: 28 additions & 0 deletions tools/sdbusplus/templates/event.cpp.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
int ${event.CamelCase}::set_error(sd_bus_error* e) const
{
if (jsonString.empty())
{
jsonString = to_json().dump();
}

return sd_bus_error_set(e, errName, jsonString.c_str());
}

int ${event.CamelCase}::set_error(SdBusInterface* i, sd_bus_error* e) const
{
if (jsonString.empty())
{
jsonString = to_json().dump();
}

return i->sd_bus_error_set(e, errName, jsonString.c_str());
}

auto ${event.CamelCase}::to_json() const -> nlohmann::json
{
nlohmann::json j = { };
% for m in event.metadata:
j["${m.SNAKE_CASE}"] = ${m.camelCase};
% endfor
return nlohmann::json{ { errName, std::move(j) } };
}
6 changes: 5 additions & 1 deletion tools/sdbusplus/templates/event.hpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ struct ${event.CamelCase} final :
{}

%for m in event.metadata:
${m.cppTypeParam(events.name)} ${m.camelCase};
const ${m.cppTypeParam(events.name)} ${m.camelCase};
%endfor

%endif
int set_error(sd_bus_error*) const override;
int set_error(SdBusInterface*, sd_bus_error*) const override;
auto to_json() const -> nlohmann::json;

static constexpr auto errName =
"${events.name}.${event.name}";
static constexpr auto errDesc =
Expand Down
25 changes: 25 additions & 0 deletions tools/sdbusplus/templates/events.cpp.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <${events.headerFile("event")}>
#include <nlohmann/json.hpp>

%if events.errors:

namespace sdbusplus::error::${events.cppNamespacedClass()}
{
% for e in events.errors:

${events.render(loader, "event.cpp.mako", events=events, event=e)}\
% endfor

} // namespace sdbusplus::error::${events.cppNamespacedClass()}
%endif
%if events.errors:

namespace sdbusplus::event::${events.cppNamespacedClass()}
{
% for e in events.events:

${events.render(loader, "event.cpp.mako", events=events, event=e)}\
% endfor

} // namespace sdbusplus::event::${events.cppNamespacedClass()}
%endif

0 comments on commit 14c4797

Please sign in to comment.