From f422aba43aaa7fe85448962129a10f5d49c376aa Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Mon, 14 Jun 2021 13:43:08 +0200
Subject: [PATCH] Handle non well defined IDs in Reader::updated() (handling
 subscriptions)

---
 .../libraries/armem/client/Reader.cpp         | 23 +++++++++++++++++--
 .../libraries/armem/core/MemoryID.cpp         |  4 ++--
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/source/RobotAPI/libraries/armem/client/Reader.cpp b/source/RobotAPI/libraries/armem/client/Reader.cpp
index fc4775bc3..72e450c42 100644
--- a/source/RobotAPI/libraries/armem/client/Reader.cpp
+++ b/source/RobotAPI/libraries/armem/client/Reader.cpp
@@ -1,5 +1,7 @@
 #include "Reader.h"
 
+#include <sstream>
+
 #include <ArmarXCore/core/logging/Logging.h>
 
 #include "query/Builder.h"
@@ -109,15 +111,27 @@ namespace armarx::armem::client
     void
     Reader::updated(const std::vector<MemoryID>& updatedSnapshotIDs) const
     {
+        std::stringstream error;
+
         for (const auto& [subscription, callbacks] : this->callbacks)
         {
             std::vector<MemoryID> matchingSnapshotIDs;
 
             for (const MemoryID& updatedSnapshotID : updatedSnapshotIDs)
             {
-                if (contains(subscription, updatedSnapshotID))
+                try
+                {
+                    if (contains(subscription, updatedSnapshotID))
+                    {
+                        matchingSnapshotIDs.push_back(updatedSnapshotID);
+                    }
+                }
+                catch (const armem::error::InvalidMemoryID& e)
                 {
-                    matchingSnapshotIDs.push_back(updatedSnapshotID);
+                    // Log to debug, but ignore otherwise
+                    error << "Error when comparing subscribed ID " << subscription
+                          << " with updated ID " << updatedSnapshotID << ":\n"
+                          << e.what() << "\n\n";
                 }
             }
 
@@ -129,6 +143,11 @@ namespace armarx::armem::client
                 }
             }
         }
+        if (error.str().size() > 0)
+        {
+            ARMARX_VERBOSE << "The following issues were encountered during Reader::" << __FUNCTION__ << "(): \n\n"
+                           << error.str();
+        }
     }
 
     data::StoreResult
diff --git a/source/RobotAPI/libraries/armem/core/MemoryID.cpp b/source/RobotAPI/libraries/armem/core/MemoryID.cpp
index 61dd4836c..30d1c1aec 100644
--- a/source/RobotAPI/libraries/armem/core/MemoryID.cpp
+++ b/source/RobotAPI/libraries/armem/core/MemoryID.cpp
@@ -418,13 +418,13 @@ namespace armarx::armem
         if (!general.isWellDefined())
         {
             std::stringstream ss;
-            ss << "ID `general` is not well-defined, which is required for `" << __FUNCTION__ << "()`.";
+            ss << "\nGeneral ID " << general << " is not well-defined, which is required for `" << __FUNCTION__ << "()`.";
             throw error::InvalidMemoryID(general, ss.str());
         }
         if (!specific.isWellDefined())
         {
             std::stringstream ss;
-            ss << "ID `specific` is not well-defined, which is required for `" << __FUNCTION__ << "()`.";
+            ss << "\nSpecific ID " << specific << " is not well-defined, which is required for `" << __FUNCTION__ << "()`.";
             throw error::InvalidMemoryID(specific, ss.str());
         }
 
-- 
GitLab