Error handling for getkwVal API
This commit updates getKwVal APIs to set error code in case of error.
This helps the caller of API to take action based on the error code
returned from the API.
Change-Id: I884dae49ebc56c57cb41f880dfc796ab53b47d16
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 af7620c..0ac08aa 100644
--- a/vpd-manager/include/error_codes.hpp
+++ b/vpd-manager/include/error_codes.hpp
@@ -34,7 +34,8 @@
STANDARD_EXCEPTION,
// VPD specific errors
- UNSUPPORTED_VPD_TYPE
+ UNSUPPORTED_VPD_TYPE,
+ KEYWORD_NOT_FOUND
};
const std::unordered_map<int, std::string> errorCodeMap = {
@@ -64,6 +65,7 @@
{error_code::GPIO_LINE_EXCEPTION, "There was an exception in GPIO line."},
{error_code::ERROR_PROCESSING_SYSTEM_CMD,
"Error while executing system command tag."},
+ {error_code::KEYWORD_NOT_FOUND, "Keyword not found"},
{error_code::UNSUPPORTED_VPD_TYPE, "This VPD type is not supported"},
{error_code::STANDARD_EXCEPTION, "Standard Exception thrown"},
{error_code::FILE_SYSTEM_ERROR, "File system error."}};
diff --git a/vpd-manager/include/utility/vpd_specific_utility.hpp b/vpd-manager/include/utility/vpd_specific_utility.hpp
index 520bf3a..acbc627 100644
--- a/vpd-manager/include/utility/vpd_specific_utility.hpp
+++ b/vpd-manager/include/utility/vpd_specific_utility.hpp
@@ -152,36 +152,32 @@
*
* @param[in] i_kwdValueMap - A map having Kwd value pair.
* @param[in] i_kwd - keyword name.
+ * @param[out] o_errCode - To set error code in case of error.
*
* @return On success returns value of the keyword read from map, otherwise
* returns empty string.
*/
inline std::string getKwVal(const types::IPZKwdValueMap& i_kwdValueMap,
- const std::string& i_kwd) noexcept
+ const std::string& i_kwd,
+ uint16_t& o_errCode) noexcept
{
std::string l_kwdValue;
- try
+ if (i_kwd.empty() || i_kwdValueMap.empty())
{
- if (i_kwd.empty())
- {
- throw std::runtime_error("Invalid parameters");
- }
+ o_errCode = error_code::INVALID_INPUT_PARAMETER;
+ return l_kwdValue;
+ }
- 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)
+ auto l_itrToKwd = i_kwdValueMap.find(i_kwd);
+ if (l_itrToKwd != i_kwdValueMap.end())
{
- logging::logMessage("Failed to get value for keyword [" + i_kwd +
- "]. Error : " + l_ex.what());
+ l_kwdValue = l_itrToKwd->second;
}
+ else
+ {
+ o_errCode = error_code::KEYWORD_NOT_FOUND;
+ }
+
return l_kwdValue;
}
@@ -378,19 +374,22 @@
if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap);
ipzVpdMap && (*ipzVpdMap).find(recordName) != (*ipzVpdMap).end())
{
+ uint16_t l_errCode = 0;
auto itrToVCEN = (*ipzVpdMap).find(recordName);
- firstKwdValue = getKwVal(itrToVCEN->second, kwd1);
+ firstKwdValue = getKwVal(itrToVCEN->second, kwd1, l_errCode);
if (firstKwdValue.empty())
{
throw std::runtime_error(
- "Failed to get value for keyword [" + kwd1 + "]");
+ "Failed to get value for keyword [" + kwd1 +
+ "], error : " + commonUtility::getErrCodeMsg(l_errCode));
}
- secondKwdValue = getKwVal(itrToVCEN->second, kwd2);
+ secondKwdValue = getKwVal(itrToVCEN->second, kwd2, l_errCode);
if (secondKwdValue.empty())
{
throw std::runtime_error(
- "Failed to get value for keyword [" + kwd2 + "]");
+ "Failed to get value for keyword [" + kwd2 +
+ "], error : " + commonUtility::getErrCodeMsg(l_errCode));
}
}
else
@@ -556,12 +555,13 @@
"VINI record not found in parsed VPD. Can't find CCIN");
}
- std::string l_ccinFromVpd{
- vpdSpecificUtility::getKwVal(l_itrToRec->second, "CC")};
+ uint16_t l_errCode = 0;
+ std::string l_ccinFromVpd{vpdSpecificUtility::getKwVal(
+ l_itrToRec->second, "CC", l_errCode)};
if (l_ccinFromVpd.empty())
{
- throw DataException(
- "Empty CCIN value in VPD map. Can't find CCIN");
+ throw DataException("Can't find CCIN, error : " +
+ commonUtility::getErrCodeMsg(l_errCode));
}
transform(l_ccinFromVpd.begin(), l_ccinFromVpd.end(),
diff --git a/vpd-manager/src/backup_restore.cpp b/vpd-manager/src/backup_restore.cpp
index 67829d3..025f738 100644
--- a/vpd-manager/src/backup_restore.cpp
+++ b/vpd-manager/src/backup_restore.cpp
@@ -302,13 +302,14 @@
if (!io_srcVpdMap.empty())
{
l_srcStrValue = vpdSpecificUtility::getKwVal(
- io_srcVpdMap.at(l_srcRecordName), l_srcKeywordName);
+ io_srcVpdMap.at(l_srcRecordName), l_srcKeywordName, l_errCode);
if (l_srcStrValue.empty())
{
std::runtime_error(
std::string("Failed to get value for keyword [") +
- l_srcKeywordName + std::string("]"));
+ l_srcKeywordName + std::string("], error : ") +
+ commonUtility::getErrCodeMsg(l_errCode));
}
l_srcBinaryValue =
@@ -334,13 +335,14 @@
if (!io_dstVpdMap.empty())
{
l_dstStrValue = vpdSpecificUtility::getKwVal(
- io_dstVpdMap.at(l_dstRecordName), l_dstKeywordName);
+ io_dstVpdMap.at(l_dstRecordName), l_dstKeywordName, l_errCode);
if (l_dstStrValue.empty())
{
std::runtime_error(
std::string("Failed to get value for keyword [") +
- l_dstKeywordName + std::string("]"));
+ l_dstKeywordName + std::string("], error : ") +
+ commonUtility::getErrCodeMsg(l_errCode));
}
l_dstBinaryValue =
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index 0c8f8f7..f81a9c1 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -836,8 +836,9 @@
return;
}
- const std::string pgKeywordValue{
- vpdSpecificUtility::getKwVal(itrToRec->second, "PG")};
+ uint16_t l_errCode = 0;
+ const std::string pgKeywordValue{vpdSpecificUtility::getKwVal(
+ itrToRec->second, "PG", l_errCode)};
if (!pgKeywordValue.empty())
{
@@ -849,8 +850,10 @@
}
else
{
- throw DataException(std::string(__FUNCTION__) +
- "Failed to get value for keyword PG");
+ throw DataException(
+ std::string(__FUNCTION__) +
+ "Failed to get value for keyword PG, error : " +
+ commonUtility::getErrCodeMsg(l_errCode));
}
}
}
@@ -909,11 +912,14 @@
return false;
}
+ uint16_t l_errCode = 0;
std::string ccinFromVpd{
- vpdSpecificUtility::getKwVal(itrToRec->second, "CC")};
+ vpdSpecificUtility::getKwVal(itrToRec->second, "CC", l_errCode)};
if (ccinFromVpd.empty())
{
+ logging::logMessage("Failed to get CCIN kwd value, error : " +
+ commonUtility::getErrCodeMsg(l_errCode));
return false;
}
@@ -1091,26 +1097,29 @@
auto l_itrToVsys = (*l_parsedVpdMap).find(constants::recVSYS);
if (l_itrToVsys != (*l_parsedVpdMap).end())
{
+ uint16_t l_errCode = 0;
const std::string l_tmKwdValue{vpdSpecificUtility::getKwVal(
- l_itrToVsys->second, constants::kwdTM)};
+ l_itrToVsys->second, constants::kwdTM, l_errCode)};
if (l_tmKwdValue.empty())
{
throw std::runtime_error(
std::string("Failed to get value for keyword [") +
constants::kwdTM +
- std::string("] while creating Asset tag."));
+ std::string("] while creating Asset tag. Error : " +
+ commonUtility::getErrCodeMsg(l_errCode)));
}
const std::string l_seKwdValue{vpdSpecificUtility::getKwVal(
- l_itrToVsys->second, constants::kwdSE)};
+ l_itrToVsys->second, constants::kwdSE, l_errCode)};
if (l_seKwdValue.empty())
{
throw std::runtime_error(
std::string("Failed to get value for keyword [") +
constants::kwdSE +
- std::string("] while creating Asset tag."));
+ std::string("] while creating Asset tag. Error : " +
+ commonUtility::getErrCodeMsg(l_errCode)));
}
l_assetTag = std::string{"Server-"} + l_tmKwdValue +