Set error code for getFruPathFromJson API
This commit updates getFruPathFromJson API to set error code in case of
error. This helps caller of API to take action based on the error code
returned from the API.
Change-Id: I66f22e997119bc93ad3ae88e21c4d4d6e8951c3f
Signed-off-by: Rekha Aparna <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/include/utility/json_utility.hpp b/vpd-manager/include/utility/json_utility.hpp
index aeeac9a..852b501 100644
--- a/vpd-manager/include/utility/json_utility.hpp
+++ b/vpd-manager/include/utility/json_utility.hpp
@@ -633,51 +633,47 @@
*
* @param[in] i_sysCfgJsonObj - System config JSON object
* @param[in] i_vpdPath - Path to where VPD is stored.
+ * @param[out] o_errCode - To set error code in case of error.
*
* @return On success return valid path, on failure return empty string.
*/
inline std::string getFruPathFromJson(const nlohmann::json& i_sysCfgJsonObj,
- const std::string& i_vpdPath) noexcept
+ const std::string& i_vpdPath,
+ uint16_t& o_errCode)
{
- try
+ if (i_vpdPath.empty())
{
- if (i_vpdPath.empty())
- {
- throw std::runtime_error("Path parameter is empty.");
- }
-
- if (!i_sysCfgJsonObj.contains("frus"))
- {
- throw std::runtime_error("Missing frus tag in system config JSON.");
- }
-
- // check if given path is FRU path
- if (i_sysCfgJsonObj["frus"].contains(i_vpdPath))
- {
- return i_vpdPath;
- }
-
- const nlohmann::json& l_fruList =
- i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>();
-
- for (const auto& l_fru : l_fruList.items())
- {
- const auto l_fruPath = l_fru.key();
-
- // check if given path is redundant FRU path or inventory path
- if (i_vpdPath == i_sysCfgJsonObj["frus"][l_fruPath].at(0).value(
- "redundantEeprom", "") ||
- (i_vpdPath == i_sysCfgJsonObj["frus"][l_fruPath].at(0).value(
- "inventoryPath", "")))
- {
- return l_fruPath;
- }
- }
+ o_errCode = error_code::INVALID_INPUT_PARAMETER;
+ return std::string{};
}
- catch (const std::exception& l_ex)
+
+ if (!i_sysCfgJsonObj.contains("frus"))
{
- logging::logMessage("Failed to get FRU path from JSON, error: " +
- std::string(l_ex.what()));
+ o_errCode = error_code::INVALID_JSON;
+ return std::string{};
+ }
+
+ // check if given path is FRU path
+ if (i_sysCfgJsonObj["frus"].contains(i_vpdPath))
+ {
+ return i_vpdPath;
+ }
+
+ const nlohmann::json& l_fruList =
+ i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>();
+
+ for (const auto& l_fru : l_fruList.items())
+ {
+ const auto l_fruPath = l_fru.key();
+
+ // check if given path is redundant FRU path or inventory path
+ if (i_vpdPath == i_sysCfgJsonObj["frus"][l_fruPath].at(0).value(
+ "redundantEeprom", "") ||
+ (i_vpdPath == i_sysCfgJsonObj["frus"][l_fruPath].at(0).value(
+ "inventoryPath", "")))
+ {
+ return l_fruPath;
+ }
}
return std::string();
@@ -847,27 +843,45 @@
types::Path l_redundantFruPath;
try
{
+ uint16_t l_errCode = 0;
+
if (!i_sysCfgJsonObj.empty())
{
// Get hardware path from system config JSON.
- const types::Path l_fruPath =
- jsonUtility::getFruPathFromJson(i_sysCfgJsonObj, io_vpdPath);
+ const types::Path l_fruPath = jsonUtility::getFruPathFromJson(
+ i_sysCfgJsonObj, io_vpdPath, l_errCode);
if (!l_fruPath.empty())
{
io_vpdPath = l_fruPath;
- uint16_t l_errCode = 0;
-
// Get inventory object path from system config JSON
l_inventoryObjPath = jsonUtility::getInventoryObjPathFromJson(
i_sysCfgJsonObj, l_fruPath, l_errCode);
+ if (l_errCode)
+ {
+ logging::logMessage(
+ "Failed to get inventory path from JSON for [" +
+ io_vpdPath + "], error : " +
+ vpdSpecificUtility::getErrCodeMsg(l_errCode));
+
+ return std::make_tuple(io_vpdPath, l_inventoryObjPath,
+ l_redundantFruPath);
+ }
+
// Get redundant hardware path if present in system config JSON
l_redundantFruPath =
jsonUtility::getRedundantEepromPathFromJson(i_sysCfgJsonObj,
l_fruPath);
}
+
+ logging::logMessage(
+ "Failed to get FRU path from JSON for [" + io_vpdPath +
+ "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
+
+ return std::make_tuple(io_vpdPath, l_inventoryObjPath,
+ l_redundantFruPath);
}
}
catch (const std::exception& l_exception)
diff --git a/vpd-manager/src/backup_restore.cpp b/vpd-manager/src/backup_restore.cpp
index 89d7d2a..0a8d844 100644
--- a/vpd-manager/src/backup_restore.cpp
+++ b/vpd-manager/src/backup_restore.cpp
@@ -154,10 +154,30 @@
return;
}
+ uint16_t l_errCode = 0;
+
const std::string l_srcFruPath =
- jsonUtility::getFruPathFromJson(m_sysCfgJsonObj, i_srcPath);
+ jsonUtility::getFruPathFromJson(m_sysCfgJsonObj, i_srcPath, l_errCode);
+
+ if (l_errCode)
+ {
+ logging::logMessage(
+ "Failed to get source FRU path for [" + i_srcPath +
+ "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
+ return;
+ }
+
const std::string l_dstFruPath =
- jsonUtility::getFruPathFromJson(m_sysCfgJsonObj, i_dstPath);
+ jsonUtility::getFruPathFromJson(m_sysCfgJsonObj, i_dstPath, l_errCode);
+
+ if (l_errCode)
+ {
+ logging::logMessage(
+ "Failed to get destination FRU path for [" + i_dstPath +
+ "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
+ return;
+ }
+
if (l_srcFruPath.empty() || l_dstFruPath.empty())
{
logging::logMessage(
@@ -165,7 +185,6 @@
return;
}
- uint16_t l_errCode = 0;
const std::string l_srcInvPath = jsonUtility::getInventoryObjPathFromJson(
m_sysCfgJsonObj, i_srcPath, l_errCode);
diff --git a/vpd-manager/src/manager.cpp b/vpd-manager/src/manager.cpp
index d7b512d..27b1f63 100644
--- a/vpd-manager/src/manager.cpp
+++ b/vpd-manager/src/manager.cpp
@@ -172,6 +172,7 @@
return -1;
}
+ uint16_t l_errCode = 0;
types::Path l_fruPath;
nlohmann::json l_sysCfgJsonObj{};
@@ -182,13 +183,20 @@
// Get the EEPROM path
if (!l_sysCfgJsonObj.empty())
{
- l_fruPath =
- jsonUtility::getFruPathFromJson(l_sysCfgJsonObj, i_vpdPath);
+ l_fruPath = jsonUtility::getFruPathFromJson(l_sysCfgJsonObj,
+ i_vpdPath, l_errCode);
}
}
if (l_fruPath.empty())
{
+ if (l_errCode)
+ {
+ logging::logMessage(
+ "Failed to get FRU path from JSON for [" + i_vpdPath +
+ "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
+ }
+
l_fruPath = i_vpdPath;
}
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index b5b3780..04f060f 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -1688,8 +1688,18 @@
throw std::runtime_error("Given DBus object path is empty.");
}
+ uint16_t l_errCode = 0;
const std::string& l_fruPath =
- jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath);
+ jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath, l_errCode);
+
+ if (l_errCode)
+ {
+ logging::logMessage(
+ "Failed to get FRU path for inventory path [" + i_dbusObjPath +
+ "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode) +
+ " Aborting FRU VPD deletion.");
+ return;
+ }
try
{
@@ -1922,6 +1932,8 @@
const sdbusplus::message::object_path& i_dbusObjPath)
{
std::string l_fruPath{};
+ uint16_t l_errCode = 0;
+
try
{
// Check if system config JSON is present
@@ -1934,11 +1946,21 @@
}
// Get FRU path for the given D-bus object path from JSON
- l_fruPath =
- jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath);
+ l_fruPath = jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath,
+ l_errCode);
if (l_fruPath.empty())
{
+ if (l_errCode)
+ {
+ logging::logMessage(
+ "Failed to get FRU path for [" +
+ std::string(i_dbusObjPath) + "], error : " +
+ vpdSpecificUtility::getErrCodeMsg(l_errCode) +
+ " Aborting single FRU VPD collection.");
+ return;
+ }
+
logging::logMessage(
"D-bus object path not present in JSON. Single FRU VPD collection is not performed for " +
std::string(i_dbusObjPath));