sdbus++: events: improve bad enum diagnostics
When unpacking an event from JSON, if a bad enum value was given
a generic STL exception resulted:
```
terminate called after throwing an instance of 'std::bad_optional_access'
```
Improve this by using the sdbusplus `InvalidEnumString` exception:
```
terminate called after throwing an instance of 'sdbusplus::exception::InvalidEnumString'
```
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I746f7265c8cf97e9a123915a123a4749d31e560c
diff --git a/tools/sdbusplus/templates/event.cpp.mako b/tools/sdbusplus/templates/event.cpp.mako
index 1473f21..f11991e 100644
--- a/tools/sdbusplus/templates/event.cpp.mako
+++ b/tools/sdbusplus/templates/event.cpp.mako
@@ -52,10 +52,17 @@
% if m.typeName == "object_path":
${m.camelCase} = self.at("${m.SNAKE_CASE}").get<std::string>();
% elif m.is_enum():
- ${m.camelCase} =
- sdbusplus::message::convert_from_string<decltype(${m.camelCase})>(
- self.at("${m.SNAKE_CASE}")
- ).value();
+ if (auto enum_value =
+ sdbusplus::message::convert_from_string<decltype(${m.camelCase})>(
+ self.at("${m.SNAKE_CASE}"));
+ enum_value.has_value())
+ {
+ ${m.camelCase} = enum_value.value();
+ }
+ else
+ {
+ throw sdbusplus::exception::InvalidEnumString();
+ }
% else:
${m.camelCase} = self.at("${m.SNAKE_CASE}");
% endif