Fix for closeSession with invalid session ID

Issue: If user trying to closeSession with invalid session ID, response
is incorrect and getting as "Unspecified error" (0xff).

Fix: Handle invalid session ID/Handle using separate try cache blocks
and correct the response. i.e. Return "invalid Session ID in request"
(0x87) if user requests to close invalid session ID. Return "invalid
Session Handle in request" (0x88) if user requests to close invalid
session Handle.

Tested:
Verified using RMCPP command.
Command : ipmitool -I lanplus -H <BMC-IP> -U <Username> -P <pwd> -C 17
          raw 0x06 0x3C 0x87 0x00 0xEC 0x8E //Close session command
Response: Unable to send RAW command (channel=0x0 netfn=0x6 lun=0x0
          cmd=0x3c rsp=0x87): Unknown (0x87)
Command : ipmitool -I lanplus -H <BMC-IP> -U <Username> -P <pwd> -C 17
          raw 0x06 0x3C 0x0 0x0 0x0 0x0 0x80
Response: Unable to send RAW command (channel=0x0 netfn=0x6 lun=0x0
          cmd=0x3c rsp=0x88): Unknown (0x88)

Signed-off-by: Jayaprakash Mutyala <mutyalax.jayaprakash@intel.com>
Change-Id: I97f57c7cc48efb17e561985b1f4964a8a15bf30e
diff --git a/command/session_cmds.cpp b/command/session_cmds.cpp
index 9330fc2..da34c3c 100644
--- a/command/session_cmds.cpp
+++ b/command/session_cmds.cpp
@@ -195,7 +195,17 @@
                 return session::ccInvalidSessionHandle;
             }
         }
+    }
+    catch (const std::exception& e)
+    {
+        log<level::ERR>("Failed to get session manager instance or sessionID "
+                        "by sessionHandle",
+                        entry("ERRMSG=%s", e.what()));
+        return session::ccInvalidSessionHandle;
+    }
 
+    try
+    {
         auto closeSessionInstance =
             session::Manager::get().getSession(reqSessionId);
         uint8_t closeSessionPriv = closeSessionInstance->currentPrivilege();
@@ -204,6 +214,16 @@
         {
             return ipmi::ccInsufficientPrivilege;
         }
+    }
+    catch (const std::exception& e)
+    {
+        log<level::ERR>("Failed to get session manager instance or sessionID",
+                        entry("ERRMSG=%s", e.what()));
+        return session::ccInvalidSessionId;
+    }
+
+    try
+    {
         status = session::Manager::get().stopSession(reqSessionId);
 
         if (!status)
@@ -213,8 +233,9 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Failed to get session manager instance",
-                        entry("ERRMSG=%s", e.what()));
+        log<level::ERR>(
+            "Failed to get session manager instance or stop session",
+            entry("ERRMSG=%s", e.what()));
         return ipmi::ccUnspecifiedError;
     }