Refactor get keyword value API exception handling
This commit refactors vpd specific utility API used to read value of a
keyword,in order to handle any exceptions thrown by it locally. All
utility methods should handle exceptions locally and log a journal log
in case of failure. The caller of the utility APIs should check the
return value to detect success/failure.
This commit also changes the caller of this API throughout the repo, in
order to check the return value.
Test:
```
- Install bitbaked image on Everest system
- After BMC boots, BMC should reach Ready state
- Check vpd-manager service status, should be active(running)
- Check no restarts in vpd-manager service
- Check vpd-manager "CollectionStatus" = "Completed"
- Check extra interfaces are processed properly
- Check backup restore working properly
```
Change-Id: I965313f512553ed5d39373dd871754e1a8fed5f3
Signed-off-by: Souvik Roy <souvikroyofficial10@gmail.com>
diff --git a/vpd-manager/include/utility/vpd_specific_utility.hpp b/vpd-manager/include/utility/vpd_specific_utility.hpp
index 09c4719..fbf713b 100644
--- a/vpd-manager/include/utility/vpd_specific_utility.hpp
+++ b/vpd-manager/include/utility/vpd_specific_utility.hpp
@@ -135,29 +135,40 @@
/**
* @brief An API to read value of a keyword.
*
- * Note: Throws exception. Caller needs to handle.
*
- * @param[in] kwdValueMap - A map having Kwd value pair.
- * @param[in] kwd - keyword name.
- * @param[out] kwdValue - Value of the keyword read from map.
+ * @param[in] i_kwdValueMap - A map having Kwd value pair.
+ * @param[in] i_kwd - keyword name.
+ *
+ * @return On success returns value of the keyword read from map, otherwise
+ * returns empty string.
*/
-inline void getKwVal(const types::IPZKwdValueMap& kwdValueMap,
- const std::string& kwd, std::string& kwdValue)
+inline std::string getKwVal(const types::IPZKwdValueMap& i_kwdValueMap,
+ const std::string& i_kwd) noexcept
{
- if (kwd.empty())
+ std::string l_kwdValue;
+ try
{
- logging::logMessage("Invalid parameters");
- throw std::runtime_error("Invalid parameters");
- }
+ if (i_kwd.empty())
+ {
+ throw std::runtime_error("Invalid parameters");
+ }
- auto itrToKwd = kwdValueMap.find(kwd);
- if (itrToKwd != kwdValueMap.end())
+ auto l_itrToKwd = i_kwdValueMap.find(i_kwd);
+ if (l_itrToKwd != i_kwdValueMap.end())
+ {
+ l_kwdValue = l_itrToKwd->second;
+ }
+ else
+ {
+ throw std::runtime_error("Keyword not found");
+ }
+ }
+ catch (const std::exception& l_ex)
{
- kwdValue = itrToKwd->second;
- return;
+ logging::logMessage("Failed to get value for keyword [" + i_kwd +
+ "]. Error : " + l_ex.what());
}
-
- throw std::runtime_error("Keyword not found");
+ return l_kwdValue;
}
/**
@@ -321,9 +332,19 @@
ipzVpdMap && (*ipzVpdMap).find(recordName) != (*ipzVpdMap).end())
{
auto itrToVCEN = (*ipzVpdMap).find(recordName);
- // The exceptions will be cautght at end.
- getKwVal(itrToVCEN->second, kwd1, firstKwdValue);
- getKwVal(itrToVCEN->second, kwd2, secondKwdValue);
+ firstKwdValue = getKwVal(itrToVCEN->second, kwd1);
+ if (firstKwdValue.empty())
+ {
+ throw std::runtime_error(
+ "Failed to get value for keyword [" + kwd1 + "]");
+ }
+
+ secondKwdValue = getKwVal(itrToVCEN->second, kwd2);
+ if (secondKwdValue.empty())
+ {
+ throw std::runtime_error(
+ "Failed to get value for keyword [" + kwd2 + "]");
+ }
}
else
{
@@ -488,9 +509,8 @@
"VINI record not found in parsed VPD. Can't find CCIN");
}
- std::string l_ccinFromVpd;
- vpdSpecificUtility::getKwVal(l_itrToRec->second, "CC",
- l_ccinFromVpd);
+ std::string l_ccinFromVpd{
+ vpdSpecificUtility::getKwVal(l_itrToRec->second, "CC")};
if (l_ccinFromVpd.empty())
{
throw DataException(