This is an automated email from the ASF dual-hosted git repository.

jt2594838 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 26583b8d0ab Fix/session c exception report (#17571)
26583b8d0ab is described below

commit 26583b8d0ab0e8e2e5ecdd9541a84dca0620730f
Author: Hongzhi Gao <[email protected]>
AuthorDate: Wed May 6 17:26:04 2026 +0800

    Fix/session c exception report (#17571)
    
    * fix cpp session error details for retried exceptions
    
    Preserve and unwrap original retry exceptions so client callers no longer 
see generic std::exception messages, and avoid exception slicing in insert 
retry handling.
    
    * add it for session c error
    
    * cpp code format
    
    * fix cpp exception rethrow to preserve detailed errors
    
    Use rethrow semantics that keep original exception type/message so 
relational query failures return server-side details instead of generic unknown 
exceptions.
---
 iotdb-client/client-cpp/src/main/Common.cpp        | 25 ++++++++++++++++++++++
 iotdb-client/client-cpp/src/main/Common.h          |  3 +++
 iotdb-client/client-cpp/src/main/Session.cpp       | 16 +++++++-------
 iotdb-client/client-cpp/src/main/Session.h         |  8 +++----
 .../client-cpp/src/main/SessionConnection.h        | 18 +++-------------
 .../src/test/cpp/sessionRelationalIT.cpp           | 23 +++++++++++++++++++-
 6 files changed, 65 insertions(+), 28 deletions(-)

diff --git a/iotdb-client/client-cpp/src/main/Common.cpp 
b/iotdb-client/client-cpp/src/main/Common.cpp
index ccdb2553831..38b913c2270 100644
--- a/iotdb-client/client-cpp/src/main/Common.cpp
+++ b/iotdb-client/client-cpp/src/main/Common.cpp
@@ -19,6 +19,31 @@
 
 #include "Common.h"
 #include <boost/date_time/gregorian/gregorian.hpp>
+#include <typeinfo>
+
+std::string extractExceptionMessage(const std::exception& exception) {
+  const char* what = exception.what();
+  if (what != nullptr) {
+    std::string message(what);
+    if (!message.empty() && message != "std::exception") {
+      return message;
+    }
+  }
+  return std::string("Unhandled exception type: ") + typeid(exception).name();
+}
+
+std::string extractExceptionMessage(const std::exception_ptr& exceptionPtr) {
+  if (exceptionPtr == nullptr) {
+    return "Unknown exception";
+  }
+  try {
+    std::rethrow_exception(exceptionPtr);
+  } catch (const std::exception& exception) {
+    return extractExceptionMessage(exception);
+  } catch (...) {
+    return "Unknown non-std exception";
+  }
+}
 
 int32_t parseDateExpressionToInt(const boost::gregorian::date& date) {
   if (date.is_not_a_date()) {
diff --git a/iotdb-client/client-cpp/src/main/Common.h 
b/iotdb-client/client-cpp/src/main/Common.h
index ab2f2562e3f..74e3f622d10 100644
--- a/iotdb-client/client-cpp/src/main/Common.h
+++ b/iotdb-client/client-cpp/src/main/Common.h
@@ -296,6 +296,9 @@ private:
   std::string message;
 };
 
+std::string extractExceptionMessage(const std::exception& exception);
+std::string extractExceptionMessage(const std::exception_ptr& exceptionPtr);
+
 class DateTimeParseException : public IoTDBException {
 private:
   std::string parsedString;
diff --git a/iotdb-client/client-cpp/src/main/Session.cpp 
b/iotdb-client/client-cpp/src/main/Session.cpp
index d1e70e747de..5e175f581b9 100644
--- a/iotdb-client/client-cpp/src/main/Session.cpp
+++ b/iotdb-client/client-cpp/src/main/Session.cpp
@@ -986,7 +986,7 @@ void Session::insertRecord(const string& deviceId, int64_t 
time, const vector<st
       } catch (RedirectException& e) {
       }
     } else {
-      throw e;
+      throw;
     }
   }
 }
@@ -1014,7 +1014,7 @@ void Session::insertRecord(const string& deviceId, 
int64_t time, const vector<st
       } catch (RedirectException& e) {
       }
     } else {
-      throw e;
+      throw;
     }
   }
 }
@@ -1040,7 +1040,7 @@ void Session::insertAlignedRecord(const string& deviceId, 
int64_t time,
       } catch (RedirectException& e) {
       }
     } else {
-      throw e;
+      throw;
     }
   }
 }
@@ -1069,7 +1069,7 @@ void Session::insertAlignedRecord(const string& deviceId, 
int64_t time,
       } catch (RedirectException& e) {
       }
     } else {
-      throw e;
+      throw;
     }
   }
 }
@@ -1237,7 +1237,7 @@ void Session::insertRecordsOfOneDevice(const string& 
deviceId, vector<int64_t>&
       } catch (RedirectException& e) {
       }
     } else {
-      throw e;
+      throw;
     }
   }
 }
@@ -1291,7 +1291,7 @@ void Session::insertAlignedRecordsOfOneDevice(const 
string& deviceId, vector<int
       } catch (RedirectException& e) {
       }
     } else {
-      throw e;
+      throw;
     }
   }
 }
@@ -1343,7 +1343,7 @@ void Session::insertTablet(TSInsertTabletReq request) {
       } catch (RedirectException& e) {
       }
     } else {
-      throw e;
+      throw;
     }
   }
 }
@@ -2019,7 +2019,7 @@ std::unique_ptr<SessionDataSet> 
Session::executeQueryStatementMayRedirect(const
     }
   } catch (exception& e) {
     log_error("Exception while executing query statement: %s", e.what());
-    throw e;
+    throw;
   }
 }
 
diff --git a/iotdb-client/client-cpp/src/main/Session.h 
b/iotdb-client/client-cpp/src/main/Session.h
index 30ba60d6cad..0ae279f795c 100644
--- a/iotdb-client/client-cpp/src/main/Session.h
+++ b/iotdb-client/client-cpp/src/main/Session.h
@@ -1066,19 +1066,19 @@ void 
Session::insertOnce(std::unordered_map<std::shared_ptr<SessionConnection>,
   auto req = insertGroup.begin()->second;
   try {
     insertConsumer(connection, req);
-  } catch (RedirectException e) {
+  } catch (const RedirectException& e) {
     for (const auto& deviceEndPoint : e.deviceEndPointMap) {
       handleRedirection(deviceEndPoint.first, deviceEndPoint.second);
     }
-  } catch (IoTDBConnectionException e) {
+  } catch (const IoTDBConnectionException& e) {
     if (endPointToSessionConnection.size() > 1) {
       removeBrokenSessionConnection(connection);
       try {
         insertConsumer(defaultSessionConnection_, req);
-      } catch (RedirectException e) {
+      } catch (const RedirectException& e) {
       }
     } else {
-      throw e;
+      throw;
     }
   }
 }
diff --git a/iotdb-client/client-cpp/src/main/SessionConnection.h 
b/iotdb-client/client-cpp/src/main/SessionConnection.h
index 031b7d3a892..45d1b55cc56 100644
--- a/iotdb-client/client-cpp/src/main/SessionConnection.h
+++ b/iotdb-client/client-cpp/src/main/SessionConnection.h
@@ -246,11 +246,7 @@ void 
SessionConnection::callWithRetryAndVerifyWithRedirection(std::function<T()>
   }
 
   if (result.getException()) {
-    try {
-      std::rethrow_exception(result.getException());
-    } catch (const std::exception& e) {
-      throw IoTDBConnectionException(e.what());
-    }
+    throw 
IoTDBConnectionException(extractExceptionMessage(result.getException()));
   }
 }
 
@@ -265,11 +261,7 @@ void 
SessionConnection::callWithRetryAndVerifyWithRedirectionForMultipleDevices(
     RpcUtils::verifySuccess(status);
   }
   if (result.getException()) {
-    try {
-      std::rethrow_exception(result.getException());
-    } catch (const std::exception& e) {
-      throw IoTDBConnectionException(e.what());
-    }
+    throw 
IoTDBConnectionException(extractExceptionMessage(result.getException()));
   }
   result.exception = nullptr;
 }
@@ -280,11 +272,7 @@ 
SessionConnection::callWithRetryAndVerify(std::function<T()> rpc) {
   auto result = callWithRetry<T>(rpc);
   RpcUtils::verifySuccess(result.getResult());
   if (result.getException()) {
-    try {
-      std::rethrow_exception(result.getException());
-    } catch (const std::exception& e) {
-      throw IoTDBConnectionException(e.what());
-    }
+    throw 
IoTDBConnectionException(extractExceptionMessage(result.getException()));
   }
   return result;
 }
diff --git a/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp 
b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp
index 678ec5f6b57..308b07b18e5 100644
--- a/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp
+++ b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp
@@ -17,9 +17,9 @@
  * under the License.
  */
 
-#include "catch.hpp"
 #include "TableSession.h"
 #include "TableSessionBuilder.h"
+#include "catch.hpp"
 #include <math.h>
 
 using namespace std;
@@ -140,6 +140,27 @@ TEST_CASE("Test insertRelationalTablet", 
"[testInsertRelationalTablet]") {
   REQUIRE(cnt == 15);
 }
 
+TEST_CASE("Query non-existent table returns detailed error", 
"[queryMissingTableError]") {
+  CaseReporter cr("queryMissingTableError");
+  session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1");
+  session->executeNonQueryStatement("USE db1");
+  session->executeNonQueryStatement("DROP TABLE IF EXISTS 
table_not_exists_for_query");
+
+  bool caught = false;
+  try {
+    session->executeQueryStatement("SELECT * FROM table_not_exists_for_query");
+  } catch (const std::exception& e) {
+    caught = true;
+    const std::string errorMessage = e.what();
+    INFO("caught error message: " << errorMessage);
+    const bool hasDetailedErrorMessage =
+        errorMessage.size() > 20 && errorMessage.find("std::exception") == 
std::string::npos;
+    REQUIRE(errorMessage != "std::exception");
+    REQUIRE(hasDetailedErrorMessage);
+  }
+  REQUIRE(caught);
+}
+
 TEST_CASE("Test RelationalTabletTsblockRead", 
"[testRelationalTabletTsblockRead]") {
   CaseReporter cr("testRelationalTabletTsblockRead");
   session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1");

Reply via email to