Handle error code in pre action
Call to execute pre action needs to handle error code to detect the type
of failure that occurred while executing the required tags.
Based on error code adequate action needs to be taken.
Change-Id: I428d8eab7d895b17a26068a116c28de1d4616628
Signed-off-by: Sunny Srivastava <sunnsr25@in.ibm.com>
diff --git a/vpd-manager/include/utility/json_utility.hpp b/vpd-manager/include/utility/json_utility.hpp
index c2bb646..aeeac9a 100644
--- a/vpd-manager/include/utility/json_utility.hpp
+++ b/vpd-manager/include/utility/json_utility.hpp
@@ -501,47 +501,35 @@
* @param[in] i_vpdFilePath - EEPROM file path
* @param[in] i_flagToProcess - To identify which flag(s) needs to be processed
* under PreAction tag of config JSON.
+ * @param[out] o_errCode - To set error code in case of error.
* @return - success or failure
*/
inline bool executeBaseAction(
const nlohmann::json& i_parsedConfigJson, const std::string& i_action,
- const std::string& i_vpdFilePath, const std::string& i_flagToProcess)
+ const std::string& i_vpdFilePath, const std::string& i_flagToProcess,
+ uint16_t& o_errCode)
{
- try
+ if (i_flagToProcess.empty() || i_action.empty() || i_vpdFilePath.empty() ||
+ !i_parsedConfigJson.contains("frus"))
{
- if (i_flagToProcess.empty() || i_action.empty() ||
- i_vpdFilePath.empty() || !i_parsedConfigJson.contains("frus"))
- {
- throw std::runtime_error(
- std::string(__FUNCTION__) + " Invalid parameter");
- }
- if (!i_parsedConfigJson["frus"].contains(i_vpdFilePath))
- {
- throw JsonException(std::string(__FUNCTION__) + " File path: " +
- i_vpdFilePath + " not found in JSON");
- }
- if (!i_parsedConfigJson["frus"][i_vpdFilePath].at(0).contains(i_action))
- {
- throw JsonException(
- std::string(__FUNCTION__) + " Action [" + i_action +
- "] not defined for file path:" + i_vpdFilePath);
- }
-
- if (!(i_parsedConfigJson["frus"][i_vpdFilePath].at(0))[i_action]
- .contains(i_flagToProcess))
- {
- throw JsonException(
- std::string(__FUNCTION__) + "Config JSON missing flag [" +
- i_flagToProcess +
- "] to execute action for path = " + i_vpdFilePath);
- }
+ o_errCode = error_code::INVALID_INPUT_PARAMETER;
+ return false;
}
- catch (const std::exception& l_ex)
+ if (!i_parsedConfigJson["frus"].contains(i_vpdFilePath))
{
- EventLogger::createSyncPel(
- EventLogger::getErrorType(l_ex), types::SeverityType::Informational,
- __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex),
- std::nullopt, std::nullopt, std::nullopt, std::nullopt);
+ o_errCode = error_code::FILE_NOT_FOUND;
+ return false;
+ }
+ if (!i_parsedConfigJson["frus"][i_vpdFilePath].at(0).contains(i_action))
+ {
+ o_errCode = error_code::MISSING_ACTION_TAG;
+ return false;
+ }
+
+ if (!(i_parsedConfigJson["frus"][i_vpdFilePath].at(0))[i_action].contains(
+ i_flagToProcess))
+ {
+ o_errCode = error_code::MISSING_FLAG;
return false;
}
@@ -554,7 +542,6 @@
auto itrToFunction = funcionMap.find(l_tag.key());
if (itrToFunction != funcionMap.end())
{
- uint16_t o_errCode = 0;
if (!itrToFunction->second(i_parsedConfigJson, i_vpdFilePath,
i_action, i_flagToProcess, o_errCode))
{
diff --git a/vpd-manager/include/worker.hpp b/vpd-manager/include/worker.hpp
index 0080696..a40ea24 100644
--- a/vpd-manager/include/worker.hpp
+++ b/vpd-manager/include/worker.hpp
@@ -386,10 +386,12 @@
* @param[in] i_vpdFilePath - Path to the EEPROM file.
* @param[in] i_flagToProcess - To identify which flag(s) needs to be
* processed under PreAction tag of config JSON.
+ * @param[out] o_errCode - To set error code in case of error.
* @return Execution status.
*/
bool processPreAction(const std::string& i_vpdFilePath,
- const std::string& i_flagToProcess);
+ const std::string& i_flagToProcess,
+ uint16_t& o_errCode);
/**
* @brief API to process postAction(base_action) defined in config JSON.
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index de14c38..b5b3780 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -1175,17 +1175,18 @@
}
bool Worker::processPreAction(const std::string& i_vpdFilePath,
- const std::string& i_flagToProcess)
+ const std::string& i_flagToProcess,
+ uint16_t& i_errCode)
{
if (i_vpdFilePath.empty() || i_flagToProcess.empty())
{
- logging::logMessage(
- "Invalid input parameter. Abort processing pre action");
+ i_errCode = error_code::INVALID_INPUT_PARAMETER;
return false;
}
if ((!jsonUtility::executeBaseAction(m_parsedJson, "preAction",
- i_vpdFilePath, i_flagToProcess)) &&
+ i_vpdFilePath, i_flagToProcess,
+ i_errCode)) &&
(i_flagToProcess.compare("collection") == constants::STR_CMP_SUCCESS))
{
// TODO: Need a way to delete inventory object from Dbus and persisted
@@ -1265,11 +1266,14 @@
}
}
+ uint16_t l_errCode = 0;
if (!jsonUtility::executeBaseAction(m_parsedJson, "postAction",
- i_vpdFruPath, i_flagToProcess))
+ i_vpdFruPath, i_flagToProcess,
+ l_errCode))
{
logging::logMessage(
- "Execution of post action failed for path: " + i_vpdFruPath);
+ "Execution of post action failed for path: " + i_vpdFruPath +
+ " . Reason: " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
// If post action was required and failed only in that case return
// false. In all other case post action is considered passed.
@@ -1295,10 +1299,23 @@
"preAction", "collection"))
{
isPreActionRequired = true;
- if (!processPreAction(i_vpdFilePath, "collection"))
+ uint16_t l_errCode = 0;
+ if (!processPreAction(i_vpdFilePath, "collection", l_errCode))
{
+ if (l_errCode == error_code::DEVICE_NOT_PRESENT)
+ {
+ logging::logMessage(
+ vpdSpecificUtility::getErrCodeMsg(l_errCode) +
+ i_vpdFilePath);
+ // Presence pin has been read successfully and has been read
+ // as false, so this is not a failure case, hence returning
+ // empty variant so that pre action is not marked as failed.
+ return types::VPDMapVariant{};
+ }
throw std::runtime_error(
- std::string(__FUNCTION__) + " Pre-Action failed");
+ std::string(__FUNCTION__) +
+ " Pre-Action failed with error: " +
+ vpdSpecificUtility::getErrCodeMsg(l_errCode));
}
}
@@ -1709,9 +1726,17 @@
if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
"preAction", "deletion"))
{
- if (!processPreAction(l_fruPath, "deletion"))
+ uint16_t l_errCode = 0;
+ if (!processPreAction(l_fruPath, "deletion", l_errCode))
{
- throw std::runtime_error("Pre action failed");
+ std::string l_msg = "Pre action failed";
+ if (l_errCode)
+ {
+ l_msg +=
+ " Reason: " +
+ vpdSpecificUtility::getErrCodeMsg(l_errCode);
+ }
+ throw std::runtime_error(l_msg);
}
}