Skip to content

Commit 26583b8

Browse files
authored
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.
1 parent 8c3d2b6 commit 26583b8

6 files changed

Lines changed: 65 additions & 28 deletions

File tree

iotdb-client/client-cpp/src/main/Common.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@
1919

2020
#include "Common.h"
2121
#include <boost/date_time/gregorian/gregorian.hpp>
22+
#include <typeinfo>
23+
24+
std::string extractExceptionMessage(const std::exception& exception) {
25+
const char* what = exception.what();
26+
if (what != nullptr) {
27+
std::string message(what);
28+
if (!message.empty() && message != "std::exception") {
29+
return message;
30+
}
31+
}
32+
return std::string("Unhandled exception type: ") + typeid(exception).name();
33+
}
34+
35+
std::string extractExceptionMessage(const std::exception_ptr& exceptionPtr) {
36+
if (exceptionPtr == nullptr) {
37+
return "Unknown exception";
38+
}
39+
try {
40+
std::rethrow_exception(exceptionPtr);
41+
} catch (const std::exception& exception) {
42+
return extractExceptionMessage(exception);
43+
} catch (...) {
44+
return "Unknown non-std exception";
45+
}
46+
}
2247

2348
int32_t parseDateExpressionToInt(const boost::gregorian::date& date) {
2449
if (date.is_not_a_date()) {

iotdb-client/client-cpp/src/main/Common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ class IoTDBException : public std::exception {
296296
std::string message;
297297
};
298298

299+
std::string extractExceptionMessage(const std::exception& exception);
300+
std::string extractExceptionMessage(const std::exception_ptr& exceptionPtr);
301+
299302
class DateTimeParseException : public IoTDBException {
300303
private:
301304
std::string parsedString;

iotdb-client/client-cpp/src/main/Session.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ void Session::insertRecord(const string& deviceId, int64_t time, const vector<st
986986
} catch (RedirectException& e) {
987987
}
988988
} else {
989-
throw e;
989+
throw;
990990
}
991991
}
992992
}
@@ -1014,7 +1014,7 @@ void Session::insertRecord(const string& deviceId, int64_t time, const vector<st
10141014
} catch (RedirectException& e) {
10151015
}
10161016
} else {
1017-
throw e;
1017+
throw;
10181018
}
10191019
}
10201020
}
@@ -1040,7 +1040,7 @@ void Session::insertAlignedRecord(const string& deviceId, int64_t time,
10401040
} catch (RedirectException& e) {
10411041
}
10421042
} else {
1043-
throw e;
1043+
throw;
10441044
}
10451045
}
10461046
}
@@ -1069,7 +1069,7 @@ void Session::insertAlignedRecord(const string& deviceId, int64_t time,
10691069
} catch (RedirectException& e) {
10701070
}
10711071
} else {
1072-
throw e;
1072+
throw;
10731073
}
10741074
}
10751075
}
@@ -1237,7 +1237,7 @@ void Session::insertRecordsOfOneDevice(const string& deviceId, vector<int64_t>&
12371237
} catch (RedirectException& e) {
12381238
}
12391239
} else {
1240-
throw e;
1240+
throw;
12411241
}
12421242
}
12431243
}
@@ -1291,7 +1291,7 @@ void Session::insertAlignedRecordsOfOneDevice(const string& deviceId, vector<int
12911291
} catch (RedirectException& e) {
12921292
}
12931293
} else {
1294-
throw e;
1294+
throw;
12951295
}
12961296
}
12971297
}
@@ -1343,7 +1343,7 @@ void Session::insertTablet(TSInsertTabletReq request) {
13431343
} catch (RedirectException& e) {
13441344
}
13451345
} else {
1346-
throw e;
1346+
throw;
13471347
}
13481348
}
13491349
}
@@ -2019,7 +2019,7 @@ std::unique_ptr<SessionDataSet> Session::executeQueryStatementMayRedirect(const
20192019
}
20202020
} catch (exception& e) {
20212021
log_error("Exception while executing query statement: %s", e.what());
2022-
throw e;
2022+
throw;
20232023
}
20242024
}
20252025

iotdb-client/client-cpp/src/main/Session.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,19 +1066,19 @@ void Session::insertOnce(std::unordered_map<std::shared_ptr<SessionConnection>,
10661066
auto req = insertGroup.begin()->second;
10671067
try {
10681068
insertConsumer(connection, req);
1069-
} catch (RedirectException e) {
1069+
} catch (const RedirectException& e) {
10701070
for (const auto& deviceEndPoint : e.deviceEndPointMap) {
10711071
handleRedirection(deviceEndPoint.first, deviceEndPoint.second);
10721072
}
1073-
} catch (IoTDBConnectionException e) {
1073+
} catch (const IoTDBConnectionException& e) {
10741074
if (endPointToSessionConnection.size() > 1) {
10751075
removeBrokenSessionConnection(connection);
10761076
try {
10771077
insertConsumer(defaultSessionConnection_, req);
1078-
} catch (RedirectException e) {
1078+
} catch (const RedirectException& e) {
10791079
}
10801080
} else {
1081-
throw e;
1081+
throw;
10821082
}
10831083
}
10841084
}

iotdb-client/client-cpp/src/main/SessionConnection.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,7 @@ void SessionConnection::callWithRetryAndVerifyWithRedirection(std::function<T()>
246246
}
247247

248248
if (result.getException()) {
249-
try {
250-
std::rethrow_exception(result.getException());
251-
} catch (const std::exception& e) {
252-
throw IoTDBConnectionException(e.what());
253-
}
249+
throw IoTDBConnectionException(extractExceptionMessage(result.getException()));
254250
}
255251
}
256252

@@ -265,11 +261,7 @@ void SessionConnection::callWithRetryAndVerifyWithRedirectionForMultipleDevices(
265261
RpcUtils::verifySuccess(status);
266262
}
267263
if (result.getException()) {
268-
try {
269-
std::rethrow_exception(result.getException());
270-
} catch (const std::exception& e) {
271-
throw IoTDBConnectionException(e.what());
272-
}
264+
throw IoTDBConnectionException(extractExceptionMessage(result.getException()));
273265
}
274266
result.exception = nullptr;
275267
}
@@ -280,11 +272,7 @@ SessionConnection::callWithRetryAndVerify(std::function<T()> rpc) {
280272
auto result = callWithRetry<T>(rpc);
281273
RpcUtils::verifySuccess(result.getResult());
282274
if (result.getException()) {
283-
try {
284-
std::rethrow_exception(result.getException());
285-
} catch (const std::exception& e) {
286-
throw IoTDBConnectionException(e.what());
287-
}
275+
throw IoTDBConnectionException(extractExceptionMessage(result.getException()));
288276
}
289277
return result;
290278
}

iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
* under the License.
1818
*/
1919

20-
#include "catch.hpp"
2120
#include "TableSession.h"
2221
#include "TableSessionBuilder.h"
22+
#include "catch.hpp"
2323
#include <math.h>
2424

2525
using namespace std;
@@ -140,6 +140,27 @@ TEST_CASE("Test insertRelationalTablet", "[testInsertRelationalTablet]") {
140140
REQUIRE(cnt == 15);
141141
}
142142

143+
TEST_CASE("Query non-existent table returns detailed error", "[queryMissingTableError]") {
144+
CaseReporter cr("queryMissingTableError");
145+
session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1");
146+
session->executeNonQueryStatement("USE db1");
147+
session->executeNonQueryStatement("DROP TABLE IF EXISTS table_not_exists_for_query");
148+
149+
bool caught = false;
150+
try {
151+
session->executeQueryStatement("SELECT * FROM table_not_exists_for_query");
152+
} catch (const std::exception& e) {
153+
caught = true;
154+
const std::string errorMessage = e.what();
155+
INFO("caught error message: " << errorMessage);
156+
const bool hasDetailedErrorMessage =
157+
errorMessage.size() > 20 && errorMessage.find("std::exception") == std::string::npos;
158+
REQUIRE(errorMessage != "std::exception");
159+
REQUIRE(hasDetailedErrorMessage);
160+
}
161+
REQUIRE(caught);
162+
}
163+
143164
TEST_CASE("Test RelationalTabletTsblockRead", "[testRelationalTabletTsblockRead]") {
144165
CaseReporter cr("testRelationalTabletTsblockRead");
145166
session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1");

0 commit comments

Comments
 (0)