Error code for getExpandedLocationCode API
This commit updates getExpnadedLocationCode API 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: I1570e13ca782f9320f1fc7e43680807d232e6310
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 2f4ccd5..91a16bc 100644
--- a/vpd-manager/include/error_codes.hpp
+++ b/vpd-manager/include/error_codes.hpp
@@ -32,11 +32,14 @@
GPIO_LINE_EXCEPTION,
ERROR_PROCESSING_SYSTEM_CMD,
STANDARD_EXCEPTION,
+ DBUS_FAILURE,
// VPD specific errors
UNSUPPORTED_VPD_TYPE,
KEYWORD_NOT_FOUND,
- OUT_OF_BOUND_EXCEPTION
+ OUT_OF_BOUND_EXCEPTION,
+ FAILED_TO_DETECT_LOCATION_CODE_TYPE,
+ RECEIVED_INVALID_KWD_TYPE_FROM_DBUS
};
const std::unordered_map<int, std::string> errorCodeMap = {
@@ -70,5 +73,10 @@
{error_code::OUT_OF_BOUND_EXCEPTION, "Out of bound error"},
{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."}};
+ {error_code::FILE_SYSTEM_ERROR, "File system error."},
+ {error_code::FAILED_TO_DETECT_LOCATION_CODE_TYPE,
+ "Failed to detect location code type"},
+ {error_code::DBUS_FAILURE, "Dbus call failed"},
+ {error_code::RECEIVED_INVALID_KWD_TYPE_FROM_DBUS,
+ "Received invalid keyword data type from DBus."}};
} // namespace vpd
diff --git a/vpd-manager/include/utility/vpd_specific_utility.hpp b/vpd-manager/include/utility/vpd_specific_utility.hpp
index f2ba545..7cb0a35 100644
--- a/vpd-manager/include/utility/vpd_specific_utility.hpp
+++ b/vpd-manager/include/utility/vpd_specific_utility.hpp
@@ -342,13 +342,21 @@
*
* @param[in] unexpandedLocationCode - Unexpanded location code.
* @param[in] parsedVpdMap - Parsed VPD map.
+ * @param[out] o_errCode - To set error code in case of error.
* @return Expanded location code. In case of any error, unexpanded is returned
* as it is.
*/
inline std::string getExpandedLocationCode(
const std::string& unexpandedLocationCode,
- const types::VPDMapVariant& parsedVpdMap)
+ const types::VPDMapVariant& parsedVpdMap, uint16_t& o_errCode)
{
+ if (unexpandedLocationCode.empty() ||
+ std::holds_alternative<std::monostate>(parsedVpdMap))
+ {
+ o_errCode = error_code::INVALID_INPUT_PARAMETER;
+ return unexpandedLocationCode;
+ }
+
auto expanded{unexpandedLocationCode};
try
@@ -381,8 +389,8 @@
}
else
{
- throw std::runtime_error(
- "Error detecting type of unexpanded location code.");
+ o_errCode = error_code::FAILED_TO_DETECT_LOCATION_CODE_TYPE;
+ return expanded;
}
}
@@ -391,22 +399,19 @@
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, l_errCode);
+ firstKwdValue = getKwVal(itrToVCEN->second, kwd1, o_errCode);
if (firstKwdValue.empty())
{
- throw std::runtime_error(
- "Failed to get value for keyword [" + kwd1 +
- "], error : " + commonUtility::getErrCodeMsg(l_errCode));
+ o_errCode = error_code::KEYWORD_NOT_FOUND;
+ return expanded;
}
- secondKwdValue = getKwVal(itrToVCEN->second, kwd2, l_errCode);
+ secondKwdValue = getKwVal(itrToVCEN->second, kwd2, o_errCode);
if (secondKwdValue.empty())
{
- throw std::runtime_error(
- "Failed to get value for keyword [" + kwd2 +
- "], error : " + commonUtility::getErrCodeMsg(l_errCode));
+ o_errCode = error_code::KEYWORD_NOT_FOUND;
+ return expanded;
}
}
else
@@ -418,7 +423,8 @@
if (mapperRetValue.empty())
{
- throw std::runtime_error("Mapper failed to get service");
+ o_errCode = error_code::DBUS_FAILURE;
+ return expanded;
}
const std::string& serviceName = std::get<0>(mapperRetValue.at(0));
@@ -435,8 +441,8 @@
}
else
{
- throw std::runtime_error(
- "Failed to read value of " + kwd1 + " from Bus");
+ o_errCode = error_code::RECEIVED_INVALID_KWD_TYPE_FROM_DBUS;
+ return expanded;
}
retVal = dbusUtility::readDbusProperty(
@@ -451,8 +457,8 @@
}
else
{
- throw std::runtime_error(
- "Failed to read value of " + kwd2 + " from Bus");
+ o_errCode = error_code::RECEIVED_INVALID_KWD_TYPE_FROM_DBUS;
+ return expanded;
}
}
@@ -470,8 +476,7 @@
}
catch (const std::exception& ex)
{
- logging::logMessage("Failed to expand location code with exception: " +
- std::string(ex.what()));
+ o_errCode = error_code::STANDARD_EXCEPTION;
}
return expanded;