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/common/instance_id.hpp b/common/instance_id.hpp
index 40de1f1..2a29f82 100644
--- a/common/instance_id.hpp
+++ b/common/instance_id.hpp
@@ -5,12 +5,70 @@
#include <cerrno>
#include <cstdint>
#include <exception>
+#include <expected>
#include <string>
#include <system_error>
namespace pldm
{
+/**
+ * @class InstanceIdError
+ * @brief Exception for PLDM instance ID allocation and management errors.
+ */
+class InstanceIdError : public std::exception
+{
+ public:
+ InstanceIdError(const InstanceIdError&) noexcept = default;
+ InstanceIdError(InstanceIdError&&) noexcept = default;
+ InstanceIdError& operator=(const InstanceIdError&) noexcept = default;
+ InstanceIdError& operator=(InstanceIdError&&) noexcept = default;
+ ~InstanceIdError() noexcept override = default;
+
+ /** @brief Construct with an error code. */
+ explicit InstanceIdError(int rc) : rc_(rc), msg_(rcToMsg(rc)) {}
+
+ /** @brief Construct with an error code and a string message (copied). */
+ InstanceIdError(int rc, const std::string& m) : rc_(rc), msg_(m) {}
+
+ /** @brief Construct with an error code and a string message (moved). */
+ InstanceIdError(int rc, std::string&& m) : rc_(rc), msg_(std::move(m)) {}
+
+ /** @brief Get the error code. */
+ int rc() const noexcept
+ {
+ return rc_;
+ }
+
+ /** @brief Get the error message. */
+ const std::string& msg() const noexcept
+ {
+ return msg_;
+ }
+
+ /** @brief Convert an error code to a message. */
+ static std::string rcToMsg(int rc)
+ {
+ switch (rc)
+ {
+ case -EAGAIN:
+ return "No free instance ids";
+ default:
+ return std::system_category().message(rc);
+ }
+ }
+
+ /** @brief Get the error message (for std::exception). */
+ const char* what() const noexcept override
+ {
+ return msg_.c_str();
+ }
+
+ private:
+ int rc_;
+ std::string msg_;
+};
+
/** @class InstanceId
* @brief Implementation of PLDM instance id as per DSP0240 v1.0.0
*/
@@ -53,22 +111,19 @@
/** @brief Allocate an instance ID for the given terminus
* @param[in] tid - the terminus ID the instance ID is associated with
- * @return - PLDM instance id or -EAGAIN if there are no available instance
- * IDs
+ * @return - PLDM instance id on success, or InstanceIdError on failure
*/
- uint8_t next(uint8_t tid)
+ std::expected<uint8_t, InstanceIdError> next(uint8_t tid)
{
uint8_t id;
int rc = pldm_instance_id_alloc(pldmInstanceIdDb, tid, &id);
- if (rc == -EAGAIN)
- {
- throw std::runtime_error("No free instance ids");
- }
-
if (rc)
{
- throw std::system_category().default_error_condition(rc);
+ return std::unexpected(
+ InstanceIdError{rc, "Failed to allocate instance ID for EID " +
+ std::to_string(tid) + ": " +
+ InstanceIdError::rcToMsg(rc)});
}
return id;
diff --git a/common/utils.hpp b/common/utils.hpp
index 358d0d7..bccd929 100644
--- a/common/utils.hpp
+++ b/common/utils.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include "instance_id.hpp"
#include "types.hpp"
#include <libpldm/base.h>
@@ -20,6 +21,7 @@
#include <cstdint>
#include <deque>
#include <exception>
+#include <expected>
#include <filesystem>
#include <iostream>
#include <map>
@@ -164,6 +166,28 @@
return bcd;
}
+/**
+ * @brief Unwraps the result of InstanceId allocation and logs errors.
+ *
+ * Logs errors if present, but always returns the original result so the caller
+ * can choose to handle the error (return, throw, etc).
+ *
+ * @tparam T Instance ID value type.
+ * @param[in] result The result from InstanceIdDb::next().
+ * @return std::expected<T, InstanceIdError>
+ * Returns the original result (value or error).
+ */
+template <typename T>
+std::expected<T, pldm::InstanceIdError> getInstanceId(
+ const std::expected<T, pldm::InstanceIdError>& result)
+{
+ if (!result)
+ {
+ std::cerr << result.error().msg() << std::endl;
+ }
+ return result;
+}
+
struct DBusMapping
{
std::string objectPath; //!< D-Bus object path