set error code for getServiceName API
This commit updates getServiceName 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: I0146ca08a9cedfcb5f77fcda6fe00637a15ab281
Signed-off-by: Rekha Aparna <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/include/error_codes.hpp b/vpd-manager/include/error_codes.hpp
index 7ba6a5c..60a1492 100644
--- a/vpd-manager/include/error_codes.hpp
+++ b/vpd-manager/include/error_codes.hpp
@@ -19,6 +19,7 @@
FRU_PATH_NOT_FOUND,
JSON_PARSE_ERROR,
JSON_MISSING_GPIO_INFO,
+ JSON_MISSING_SERVICE_NAME,
// Generic errors.
INVALID_INPUT_PARAMETER,
@@ -42,6 +43,8 @@
{error_code::INVALID_INPUT_PARAMETER,
"Either one of the input parameter is invalid or empty."},
{error_code::JSON_MISSING_GPIO_INFO, "JSON missing required GPIO info."},
+ {error_code::JSON_MISSING_SERVICE_NAME,
+ "JSON missing the service name for the FRU"},
{error_code::DEVICE_NOT_PRESENT,
"Presence pin read successfully but device was absent."},
{error_code::DEVICE_PRESENCE_UNKNOWN, "Exception on presence line GPIO."},
diff --git a/vpd-manager/include/utility/json_utility.hpp b/vpd-manager/include/utility/json_utility.hpp
index d021f81..ea92137 100644
--- a/vpd-manager/include/utility/json_utility.hpp
+++ b/vpd-manager/include/utility/json_utility.hpp
@@ -907,6 +907,7 @@
*
* @param[in] i_sysCfgJsonObj - System config JSON object.
* @param[in] l_inventoryPath - DBus inventory path.
+ * @param[out] o_errCode - To set error code in case of error.
*
* @return On success returns the service name present in the system config
* JSON, otherwise empty string.
@@ -914,44 +915,43 @@
* Note: Caller has to handle in case of empty string received.
*/
inline std::string getServiceName(const nlohmann::json& i_sysCfgJsonObj,
- const std::string& l_inventoryPath)
+ const std::string& l_inventoryPath,
+ uint16_t& o_errCode)
{
- try
+ if (l_inventoryPath.empty())
{
- if (l_inventoryPath.empty())
- {
- throw std::runtime_error("Path parameter is empty.");
- }
+ o_errCode = error_code::INVALID_INPUT_PARAMETER;
+ return std::string{};
+ }
- if (!i_sysCfgJsonObj.contains("frus"))
- {
- throw std::runtime_error("Missing frus tag in system config JSON.");
- }
+ if (!i_sysCfgJsonObj.contains("frus"))
+ {
+ o_errCode = error_code::INVALID_JSON;
+ return std::string{};
+ }
- const nlohmann::json& l_listOfFrus =
- i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>();
+ const nlohmann::json& l_listOfFrus =
+ i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>();
- for (const auto& l_frus : l_listOfFrus.items())
+ for (const auto& l_frus : l_listOfFrus.items())
+ {
+ for (const auto& l_inventoryItem : l_frus.value())
{
- for (const auto& l_inventoryItem : l_frus.value())
+ if (l_inventoryPath.compare(l_inventoryItem["inventoryPath"]) ==
+ constants::STR_CMP_SUCCESS)
{
- if (l_inventoryPath.compare(l_inventoryItem["inventoryPath"]) ==
- constants::STR_CMP_SUCCESS)
+ if (l_inventoryItem.contains("serviceName"))
{
- return l_inventoryItem["serviceName"];
+ return l_inventoryItem.value("serviceName", "");
}
+
+ o_errCode = error_code::JSON_MISSING_SERVICE_NAME;
+ return std::string{};
}
}
- throw std::runtime_error(
- "Inventory path not found in the system config JSON");
}
- catch (const std::exception& l_exception)
- {
- logging::logMessage(
- "Error while getting DBus service name for given path " +
- l_inventoryPath + ", error: " + std::string(l_exception.what()));
- // TODO:log PEL
- }
+
+ o_errCode = error_code::FRU_PATH_NOT_FOUND;
return std::string{};
}
diff --git a/vpd-manager/src/backup_restore.cpp b/vpd-manager/src/backup_restore.cpp
index 0a8d844..fd7b03b 100644
--- a/vpd-manager/src/backup_restore.cpp
+++ b/vpd-manager/src/backup_restore.cpp
@@ -220,13 +220,24 @@
}
const std::string l_srcServiceName =
- jsonUtility::getServiceName(m_sysCfgJsonObj, l_srcInvPath);
- const std::string l_dstServiceName =
- jsonUtility::getServiceName(m_sysCfgJsonObj, l_dstInvPath);
- if (l_srcServiceName.empty() || l_dstServiceName.empty())
+ jsonUtility::getServiceName(m_sysCfgJsonObj, l_srcInvPath, l_errCode);
+
+ if (l_errCode)
{
logging::logMessage(
- "Couldn't find either source or destination DBus service name.");
+ "Failed to get service name for source FRU [" + l_srcInvPath +
+ "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
+ return;
+ }
+
+ const std::string l_dstServiceName =
+ jsonUtility::getServiceName(m_sysCfgJsonObj, l_dstInvPath, l_errCode);
+
+ if (l_errCode)
+ {
+ logging::logMessage(
+ "Failed to get service name for destination FRU [" + l_dstInvPath +
+ "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
return;
}