Error code for encodeKeyword API

This commit updates encodeKeyword 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: I0cba6191d2ec6ded8e793514086f3e62218cf893
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 0ac08aa..2f4ccd5 100644
--- a/vpd-manager/include/error_codes.hpp
+++ b/vpd-manager/include/error_codes.hpp
@@ -35,7 +35,8 @@
 
     // VPD specific errors
     UNSUPPORTED_VPD_TYPE,
-    KEYWORD_NOT_FOUND
+    KEYWORD_NOT_FOUND,
+    OUT_OF_BOUND_EXCEPTION
 };
 
 const std::unordered_map<int, std::string> errorCodeMap = {
@@ -66,6 +67,7 @@
     {error_code::ERROR_PROCESSING_SYSTEM_CMD,
      "Error while executing system command tag."},
     {error_code::KEYWORD_NOT_FOUND, "Keyword not found"},
+    {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."}};
diff --git a/vpd-manager/include/utility/vpd_specific_utility.hpp b/vpd-manager/include/utility/vpd_specific_utility.hpp
index 73cee0e..e744f16 100644
--- a/vpd-manager/include/utility/vpd_specific_utility.hpp
+++ b/vpd-manager/include/utility/vpd_specific_utility.hpp
@@ -186,14 +186,28 @@
  *
  * @param[in] i_keyword - Keyword to be processed.
  * @param[in] i_encoding - Type of encoding.
+ * @param[out] o_errCode - To set error code in case of error.
  *
  * @return Value after being processed for encoded type.
  */
 inline std::string encodeKeyword(const std::string& i_keyword,
-                                 const std::string& i_encoding) noexcept
+                                 const std::string& i_encoding,
+                                 uint16_t& o_errCode) noexcept
 {
+    if (i_keyword.empty())
+    {
+        o_errCode = error_code::INVALID_INPUT_PARAMETER;
+        return std::string{};
+    }
+
     // Default value is keyword value
     std::string l_result(i_keyword.begin(), i_keyword.end());
+
+    if (i_encoding.empty())
+    {
+        return l_result;
+    }
+
     try
     {
         if (i_encoding == "MAC")
@@ -205,7 +219,8 @@
 
             if (!l_hexValue)
             {
-                throw std::runtime_error("Out of bound error");
+                o_errCode = error_code::OUT_OF_BOUND_EXCEPTION;
+                return std::string{};
             }
 
             l_result += l_hexValue;
@@ -214,7 +229,8 @@
 
             if (!l_hexValue)
             {
-                throw std::runtime_error("Out of bound error");
+                o_errCode = error_code::OUT_OF_BOUND_EXCEPTION;
+                return std::string{};
             }
 
             l_result += l_hexValue;
@@ -227,7 +243,8 @@
 
                 if (!l_hexValue)
                 {
-                    throw std::runtime_error("Out of bound error");
+                    o_errCode = error_code::OUT_OF_BOUND_EXCEPTION;
+                    return std::string{};
                 }
 
                 l_result += l_hexValue;
@@ -236,7 +253,8 @@
 
                 if (!l_hexValue)
                 {
-                    throw std::runtime_error("Out of bound error");
+                    o_errCode = error_code::OUT_OF_BOUND_EXCEPTION;
+                    return std::string{};
                 }
 
                 l_result += l_hexValue;
@@ -263,8 +281,7 @@
     catch (const std::exception& l_ex)
     {
         l_result.clear();
-        logging::logMessage("Failed to encode keyword [" + i_keyword +
-                            "]. Error: " + l_ex.what());
+        o_errCode = error_code::STANDARD_EXCEPTION;
     }
 
     return l_result;
@@ -999,19 +1016,30 @@
                                 std::get<1>(*l_ipzData));
                 });
 
+            uint16_t l_errCode = 0;
+
             if (l_matchPropValuePairIt !=
                 l_interfacesPropPair.value().items().end())
             {
+                std::string l_kwd = std::string(std::get<2>(*l_ipzData).begin(),
+                                                std::get<2>(*l_ipzData).end());
+
+                std::string l_encodedValue = vpdSpecificUtility::encodeKeyword(
+                    l_kwd, l_matchPropValuePairIt.value().value("encoding", ""),
+                    l_errCode);
+
+                if (l_errCode)
+                {
+                    logging::logMessage(
+                        "Failed to get encoded value for key : " + l_kwd +
+                        " ,error : " + commonUtility::getErrCodeMsg(l_errCode));
+                }
+
                 // add property map to interface map
                 l_interfaceMap.emplace(
                     l_interfacesPropPair.key(),
                     types::PropertyMap{
-                        {l_matchPropValuePairIt.key(),
-                         vpdSpecificUtility::encodeKeyword(
-                             std::string(std::get<2>(*l_ipzData).begin(),
-                                         std::get<2>(*l_ipzData).end()),
-                             l_matchPropValuePairIt.value().value("encoding",
-                                                                  ""))}});
+                        {l_matchPropValuePairIt.key(), l_encodedValue}});
             }
         };