pldmd/invoker: Bounds check on handler map

Instead of causing an exception and handling it, we can just bounds
check on the command handler map and return an appropriate PLDM error
code to the user.

Signed-off-by: Joe Komlodi <komlodi@google.com>
Change-Id: I4d9895dcf7fbd6b0820a075e900fe070882c2090
diff --git a/pldmd/invoker.hpp b/pldmd/invoker.hpp
index ddfb99d..34148fb 100644
--- a/pldmd/invoker.hpp
+++ b/pldmd/invoker.hpp
@@ -40,6 +40,11 @@
     Response handle(pldm_tid_t tid, Type pldmType, Command pldmCommand,
                     const pldm_msg* request, size_t reqMsgLen)
     {
+        if (handlers.find(pldmType) == handlers.end())
+        {
+            return CmdHandler::ccOnlyResponse(request,
+                                              PLDM_ERROR_INVALID_PLDM_TYPE);
+        }
         return handlers.at(pldmType)->handle(tid, pldmCommand, request,
                                              reqMsgLen);
     }
diff --git a/test/pldmd_registration_test.cpp b/test/pldmd_registration_test.cpp
index dbdad5a..e98bb21 100644
--- a/test/pldmd_registration_test.cpp
+++ b/test/pldmd_registration_test.cpp
@@ -53,8 +53,16 @@
 TEST(Registration, testFailure)
 {
     Invoker invoker{};
-    ASSERT_THROW(invoker.handle(tid, testType, testCmd, nullptr, 0),
-                 std::out_of_range);
+    const Response kExpectedBadTypeResponse = {0x01, 0x02, 0x03,
+                                               PLDM_ERROR_INVALID_PLDM_TYPE};
+    const std::array<uint8_t, sizeof(pldm_msg)> kDummyPldmRequestBacking = {
+        0x01, 0x02, 0x03};
+    const pldm_msg* kDummyPldmRequest =
+        reinterpret_cast<const pldm_msg*>(kDummyPldmRequestBacking.data());
+
+    EXPECT_EQ(invoker.handle(tid, testType, testCmd, kDummyPldmRequest, 0),
+              kExpectedBadTypeResponse);
+
     invoker.registerHandler(testType, std::make_unique<TestHandler>());
     uint8_t badCmd = 0xFE;
     ASSERT_THROW(invoker.handle(tid, testType, badCmd, nullptr, 0),