Error code for dumpBadVpd and generateBadVPDFile
This commit updates dumpBadVpd and generateBadVPDFileName APIs 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: Ibdfca73fa97d4061a120bd8dfe62fc2e929b88f9
Signed-off-by: Rekha Aparna <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/include/utility/vpd_specific_utility.hpp b/vpd-manager/include/utility/vpd_specific_utility.hpp
index 248fa50..520bf3a 100644
--- a/vpd-manager/include/utility/vpd_specific_utility.hpp
+++ b/vpd-manager/include/utility/vpd_specific_utility.hpp
@@ -29,14 +29,22 @@
* For spi eeproms - the pattern of the vpd-name will be spi-<spi-number>.
*
* @param[in] i_vpdFilePath - file path of the vpd.
+ * @param[out] o_errCode - to set error code in case of error.
*
* @return On success, returns generated file name, otherwise returns empty
* string.
*/
-inline std::string generateBadVPDFileName(
- const std::string& i_vpdFilePath) noexcept
+inline std::string generateBadVPDFileName(const std::string& i_vpdFilePath,
+ uint16_t& o_errCode) noexcept
{
std::string l_badVpdFileName{constants::badVpdDir};
+
+ if (i_vpdFilePath.empty())
+ {
+ o_errCode = error_code::INVALID_INPUT_PARAMETER;
+ return l_badVpdFileName;
+ }
+
try
{
if (i_vpdFilePath.find("i2c") != std::string::npos)
@@ -62,8 +70,7 @@
catch (const std::exception& l_ex)
{
l_badVpdFileName.clear();
- logging::logMessage("Failed to generate bad VPD file name for [" +
- i_vpdFilePath + "]. Error: " + l_ex.what());
+ o_errCode = error_code::STANDARD_EXCEPTION;
}
return l_badVpdFileName;
}
@@ -77,21 +84,35 @@
*
* @param[in] i_vpdFilePath - vpd file path
* @param[in] i_vpdVector - vpd vector
+ * @param[out] o_errCode - To set error code in case of error.
*
* @return On success returns 0, otherwise returns -1.
*/
inline int dumpBadVpd(const std::string& i_vpdFilePath,
- const types::BinaryVector& i_vpdVector) noexcept
+ const types::BinaryVector& i_vpdVector,
+ uint16_t& o_errCode) noexcept
{
+ if (i_vpdFilePath.empty() || i_vpdVector.empty())
+ {
+ o_errCode = error_code::INVALID_INPUT_PARAMETER;
+ return constants::FAILURE;
+ }
+
int l_rc{constants::FAILURE};
try
{
std::filesystem::create_directory(constants::badVpdDir);
- auto l_badVpdPath = generateBadVPDFileName(i_vpdFilePath);
+ auto l_badVpdPath = generateBadVPDFileName(i_vpdFilePath, o_errCode);
if (l_badVpdPath.empty())
{
- throw std::runtime_error("Failed to generate bad VPD file name");
+ if (o_errCode)
+ {
+ logging::logMessage("Failed to create bad VPD file name : " +
+ commonUtility::getErrCodeMsg(o_errCode));
+ }
+
+ return constants::FAILURE;
}
if (std::filesystem::exists(l_badVpdPath))
@@ -100,24 +121,16 @@
std::filesystem::remove(l_badVpdPath, l_ec);
if (l_ec) // error code
{
- const std::string l_errorMsg{
- "Error removing the existing broken vpd in " +
- l_badVpdPath +
- ". Error code : " + std::to_string(l_ec.value()) +
- ". Error message : " + l_ec.message()};
-
- throw std::runtime_error(l_errorMsg);
+ o_errCode = error_code::FILE_SYSTEM_ERROR;
+ return constants::FAILURE;
}
}
std::ofstream l_badVpdFileStream(l_badVpdPath, std::ofstream::binary);
if (!l_badVpdFileStream.is_open())
{
- const std::string l_errorMsg{
- "Failed to open bad vpd file path [" + l_badVpdPath +
- "]. Unable to dump the broken/bad vpd file."};
-
- throw std::runtime_error(l_errorMsg);
+ o_errCode = error_code::FILE_ACCESS_ERROR;
+ return constants::FAILURE;
}
l_badVpdFileStream.write(
@@ -128,8 +141,7 @@
}
catch (const std::exception& l_ex)
{
- logging::logMessage("Failed to dump bad VPD for [" + i_vpdFilePath +
- "]. Error: " + l_ex.what());
+ o_errCode = error_code::STANDARD_EXCEPTION;
}
return l_rc;
}