pldm: use std::expected for instance ID allocation
Refactor InstanceIdDb::next() to return
std::expected<uint8_t, InstanceIdError> instead of throwing exceptions.
This change enables callers to explicitly handle allocation errors via
value inspection, rather than relying on exception handling.
This approach prevents core dumps from uncaught exceptions and
eliminates the need for pervasive try-catch blocks.
Callers can now access the error code and message directly, improving
clarity and control of error propagation.
Note:
Errors from InstanceIdDb::next() are currently handled via early
return, which may silently discard failures.
This is a temporary solution; APIs and callers will need to be updated
in the future to propagate and handle errors explicitly.
Change-Id: Ibf2e0034b0ee725cb59adfd93b74e48db8c42cba
Signed-off-by: Eric Yang <eric.yang.wiwynn@gmail.com>
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 7a3b63c..601964b 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -980,7 +980,12 @@
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
auto request = new (requestMsg.data()) pldm_msg;
- auto instanceId = instanceIdDb->next(eid);
+ auto instanceIdResult = pldm::utils::getInstanceId(instanceIdDb->next(eid));
+ if (!instanceIdResult)
+ {
+ return;
+ }
+ auto instanceId = instanceIdResult.value();
uint8_t eventMessageGlobalEnable =
PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;