associations: Handle SdBusError exception

In checkIfPendingAssociation() it could create new interface on DBus
objects. The interface::interface() could throw if it fails, and mapperx
would crash due to the exception:

    phosphor-mapper[428]: terminate called after throwing an instance of 'sdbusplus::exception::SdBusError'
    phosphor-mapper[428]:   what():  sd_bus_add_object_vtable: org.freedesktop.DBus.Error.InvalidArgs: Invalid argument

Workaround this issue by adding try-catch and skip the interface to
prevent mapperx from crash.

Fixes openbmc/phosphor-objmgr#21

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: Ibe3e887dcd95c01e1270bf218fd47572852e1e54
diff --git a/src/associations.cpp b/src/associations.cpp
index 110c6eb..2c1cfa5 100644
--- a/src/associations.cpp
+++ b/src/associations.cpp
@@ -2,6 +2,7 @@
 
 #include <boost/algorithm/string/predicate.hpp>
 #include <iostream>
+#include <sdbusplus/exception.hpp>
 
 void removeAssociation(const std::string& sourcePath, const std::string& owner,
                        sdbusplus::asio::object_server& server,
@@ -396,14 +397,28 @@
         auto assocPath = objectPath + '/' + std::get<forwardTypePos>(e);
         auto endpointPath = ownerPath;
 
-        addSingleAssociation(server, assocPath, endpointPath, owner, ownerPath,
-                             assocMaps);
+        try
+        {
+            addSingleAssociation(server, assocPath, endpointPath, owner,
+                                 ownerPath, assocMaps);
 
-        // Now the reverse direction (still the same owner and ownerPath)
-        assocPath = endpointPath + '/' + std::get<reverseTypePos>(e);
-        endpointPath = objectPath;
-        addSingleAssociation(server, assocPath, endpointPath, owner, ownerPath,
-                             assocMaps);
+            // Now the reverse direction (still the same owner and ownerPath)
+            assocPath = endpointPath + '/' + std::get<reverseTypePos>(e);
+            endpointPath = objectPath;
+            addSingleAssociation(server, assocPath, endpointPath, owner,
+                                 ownerPath, assocMaps);
+        }
+        catch (const sdbusplus::exception::exception& e)
+        {
+            // In some case the interface could not be created on DBus and an
+            // exception is thrown. mapper has no control of the interface/path
+            // of the associations, so it has to catch the error and drop the
+            // association request.
+            fprintf(stderr,
+                    "Error adding association: assocPath %s, endpointPath %s, "
+                    "what: %s\n",
+                    assocPath.c_str(), endpointPath.c_str(), e.what());
+        }
 
         // Not pending anymore
         endpoint = pending->second.erase(endpoint);