API to get error type and error msg

Error type to be set should be strictly dependent on the type of
exception being caught. So that correct interface can be called from
message registry while logging a PEL.
Also, the error message logged should have a uniform format across
different kind of errors.

The commit implements API to get the pre-defined error type and message
format for a given exception.
The user needs to call this API and need not set error type on wish.

Change-Id: I3aeddd2986f12ff18b3a061533490db20ebede34
Signed-off-by: Sunny Srivastava <sunnsr25@in.ibm.com>
diff --git a/vpd-manager/include/event_logger.hpp b/vpd-manager/include/event_logger.hpp
index 7c466ce..aec8d30 100644
--- a/vpd-manager/include/event_logger.hpp
+++ b/vpd-manager/include/event_logger.hpp
@@ -21,6 +21,26 @@
 {
   public:
     /**
+     * @brief API to get Error type.
+     *
+     * @param[in] i_exception - Exception object.
+     *
+     * @return Error type set for the exception.
+     * types::ErrorType::InternalFailure otherwise.
+     */
+    static types::ErrorType getErrorType(const std::exception& i_exception);
+
+    /**
+     * @brief API to get Error msg.
+     *
+     * @param[in] i_exception - Exception object.
+     *
+     * @return Error msg set for the specific exception. Default error msg
+     * otherwise.
+     */
+    static std::string getErrorMsg(const std::exception& i_exception);
+
+    /**
      * @brief An API to create a PEL with inventory path callout.
      *
      * This API calls an async method to create PEL, and also handles inventory
diff --git a/vpd-manager/src/event_logger.cpp b/vpd-manager/src/event_logger.cpp
index 893f4e1..77de7c5 100644
--- a/vpd-manager/src/event_logger.cpp
+++ b/vpd-manager/src/event_logger.cpp
@@ -370,4 +370,43 @@
     }
     return l_errorInfo;
 }
+
+types::ErrorType EventLogger::getErrorType(const std::exception& i_exception)
+{
+    const auto& l_exceptionDataMap = getExceptionData(i_exception);
+
+    auto l_itrToErrType = l_exceptionDataMap.find("ErrorType");
+    if (l_itrToErrType == l_exceptionDataMap.end())
+    {
+        return types::ErrorType::InternalFailure;
+    }
+
+    auto l_ptrToErrType =
+        std::get_if<types::ErrorType>(&l_itrToErrType->second);
+    if (!l_ptrToErrType)
+    {
+        return types::ErrorType::InternalFailure;
+    }
+
+    return *l_ptrToErrType;
+}
+
+std::string EventLogger::getErrorMsg(const std::exception& i_exception)
+{
+    const auto& l_exceptionDataMap = getExceptionData(i_exception);
+
+    auto l_itrToErrMsg = l_exceptionDataMap.find("ErrorMsg");
+    if (l_itrToErrMsg == l_exceptionDataMap.end())
+    {
+        return i_exception.what();
+    }
+
+    auto l_ptrToErrMsg = std::get_if<std::string>(&l_itrToErrMsg->second);
+    if (!l_ptrToErrMsg)
+    {
+        return i_exception.what();
+    }
+
+    return *l_ptrToErrMsg;
+}
 } // namespace vpd
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index 4bd61bf..6506aba 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -1704,11 +1704,11 @@
     catch (const std::exception& l_ex)
     {
         EventLogger::createSyncPel(
-            types::ErrorType::InvalidVpdMessage,
-            types::SeverityType::Informational, __FILE__, __FUNCTION__, 0,
+            EventLogger::getErrorType(l_ex), types::SeverityType::Informational,
+            __FILE__, __FUNCTION__, 0,
             std::string(
                 "Exception caught while backup and restore VPD keyword's.") +
-                l_ex.what(),
+                EventLogger::getErrorMsg(l_ex),
             std::nullopt, std::nullopt, std::nullopt, std::nullopt);
     }
 }