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/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