Bladeren bron

fix(storage): convert upstream NOT_FOUND into a Result error

smartbotic-database 2.4.2 makes Find on an unknown collection or view
return NOT_FOUND, and the client library throws std::runtime_error
instead of returning an empty result. StorageClient had no try/catch
anywhere, so the exception would have propagated straight out of
query() - past its Result<QueryResult> contract - and into
execution_controller and workflow_engine, neither of which catch.

Wrap the findWithMetrics call and map the throw onto
ErrorCode::CollectionNotFound so query() keeps its contract: callers
check the Result, they never see an exception.

Requires smartbotic-database >= 2.4.2. Harmless against older servers,
which simply never throw.
fszontagh 1 maand geleden
bovenliggende
commit
36856e1d5a
2 gewijzigde bestanden met toevoegingen van 15 en 1 verwijderingen
  1. 12 1
      lib/storage/storage_client.cpp
  2. 3 0
      lib/storage/storage_client.hpp

+ 12 - 1
lib/storage/storage_client.cpp

@@ -162,7 +162,18 @@ Result<QueryResult> StorageClient::query(const std::string& collection, const Qu
     up.limit = static_cast<uint32_t>(opts.page_size);
     up.offset = static_cast<uint32_t>(std::max(0, (opts.page - 1) * opts.page_size));
 
-    auto fr = impl_->client_->findWithMetrics(collection, up);
+    // smartbotic-database >= 2.4.2 throws on an unknown collection or view
+    // instead of returning an empty result, so the miss is not mistaken for
+    // an empty collection. Convert it to our Result contract - callers of
+    // query() expect an Error, never an exception.
+    dbc::Client::FindResult fr;
+    try {
+        fr = impl_->client_->findWithMetrics(collection, up);
+    } catch (const std::exception& e) {
+        return Error(ErrorCode::CollectionNotFound,
+                     "Query failed for '" + collection + "': " + e.what());
+    }
+
     QueryResult out;
     out.documents = std::move(fr.documents);
     out.total_count = static_cast<int64_t>(fr.totalCount);

+ 3 - 0
lib/storage/storage_client.hpp

@@ -140,6 +140,9 @@ public:
                                 const std::string& id,
                                 int64_t expected_version = 0);
 
+    // Against smartbotic-database >= 2.4.2 an unknown collection or view is
+    // reported as ErrorCode::CollectionNotFound rather than an empty result.
+    // Check the Result - do not read an empty `documents` as "no rows".
     common::Result<QueryResult> query(const std::string& collection);
     common::Result<QueryResult> query(const std::string& collection, const QueryOptions& options);