@@ -44,6 +44,13 @@ inline void logAndThrow(const std::string& error_msg) {
4444 throw std::runtime_error (error_msg);
4545}
4646
47+ template <typename T>
48+ std::future<T> makeReadyFuture (T value) {
49+ std::promise<T> promise;
50+ promise.set_value (std::move (value));
51+ return promise.get_future ();
52+ }
53+
4754// Helper for debug logging of optional values
4855const auto optional_to_string = [](const auto & value) -> std::string {
4956 if (!value) {
@@ -1185,24 +1192,25 @@ std::future<void> FfiClient::sendStreamTrailerAsync(std::uint64_t local_particip
11851192 return fut;
11861193}
11871194
1188- std::future<void > FfiClient::defineSchemaAsync (std::uint64_t local_participant_handle,
1189- const DataTrackSchemaId& schema_id, const std::string& definition) {
1195+ std::future<Result<void , std::string>> FfiClient::defineSchemaAsync (std::uint64_t local_participant_handle,
1196+ const DataTrackSchemaId& schema_id,
1197+ const std::string& definition) {
11901198 // Generate client-side async_id first
11911199 const AsyncId async_id = generateAsyncId ();
11921200
11931201 // Register the async handler BEFORE sending the request
1194- auto fut = registerAsync<void >(
1202+ auto fut = registerAsync<Result< void , std::string> >(
11951203 async_id,
11961204 [async_id](const proto::FfiEvent& event) {
11971205 return event.has_define_schema () && event.define_schema ().async_id () == async_id;
11981206 },
1199- [](const proto::FfiEvent& event, std::promise<void >& pr) {
1207+ [](const proto::FfiEvent& event, std::promise<Result< void , std::string> >& pr) {
12001208 const auto & cb = event.define_schema ();
12011209 if (cb.has_error () && !cb.error ().empty ()) {
1202- pr.set_exception ( std::make_exception_ptr ( std::runtime_error (cb.error () )));
1210+ pr.set_value (Result< void , std::string>:: failure (cb.error ()));
12031211 return ;
12041212 }
1205- pr.set_value ();
1213+ pr.set_value (Result< void , std::string>:: success () );
12061214 });
12071215
12081216 // Build and send the request
@@ -1216,35 +1224,36 @@ std::future<void> FfiClient::defineSchemaAsync(std::uint64_t local_participant_h
12161224 try {
12171225 const proto::FfiResponse resp = sendRequest (req);
12181226 if (!resp.has_define_schema ()) {
1219- logAndThrow (" FfiResponse missing define_schema" );
1227+ cancelPendingByAsyncId (async_id);
1228+ return makeReadyFuture (Result<void , std::string>::failure (" FfiResponse missing define_schema" ));
12201229 }
1221- } catch (... ) {
1230+ } catch (const std::exception& e ) {
12221231 cancelPendingByAsyncId (async_id);
1223- throw ;
1232+ return makeReadyFuture (Result< void , std::string>:: failure (e. what ())) ;
12241233 }
12251234
12261235 return fut;
12271236}
12281237
1229- std::future<std::string> FfiClient::getSchemaAsync (std::uint64_t local_participant_handle,
1230- const DataTrackSchemaId& schema_id,
1231- const std::string& participant_identity) {
1238+ std::future<Result< std::string, std::string> > FfiClient::getSchemaAsync (std::uint64_t local_participant_handle,
1239+ const DataTrackSchemaId& schema_id,
1240+ const std::string& participant_identity) {
12321241 // Generate client-side async_id first
12331242 const AsyncId async_id = generateAsyncId ();
12341243
12351244 // Register the async handler BEFORE sending the request
1236- auto fut = registerAsync<std::string>(
1245+ auto fut = registerAsync<Result< std::string, std::string> >(
12371246 async_id,
12381247 [async_id](const proto::FfiEvent& event) {
12391248 return event.has_get_schema () && event.get_schema ().async_id () == async_id;
12401249 },
1241- [](const proto::FfiEvent& event, std::promise<std::string>& pr) {
1250+ [](const proto::FfiEvent& event, std::promise<Result< std::string, std::string> >& pr) {
12421251 const auto & cb = event.get_schema ();
12431252 if (cb.has_error () && !cb.error ().empty ()) {
1244- pr.set_exception ( std::make_exception_ptr ( std::runtime_error (cb.error () )));
1253+ pr.set_value (Result< std::string, std::string>:: failure (cb.error ()));
12451254 return ;
12461255 }
1247- pr.set_value (cb.definition ());
1256+ pr.set_value (Result<std::string, std::string>:: success ( cb.definition () ));
12481257 });
12491258
12501259 // Build and send the request
@@ -1258,11 +1267,12 @@ std::future<std::string> FfiClient::getSchemaAsync(std::uint64_t local_participa
12581267 try {
12591268 const proto::FfiResponse resp = sendRequest (req);
12601269 if (!resp.has_get_schema ()) {
1261- logAndThrow (" FfiResponse missing get_schema" );
1270+ cancelPendingByAsyncId (async_id);
1271+ return makeReadyFuture (Result<std::string, std::string>::failure (" FfiResponse missing get_schema" ));
12621272 }
1263- } catch (... ) {
1273+ } catch (const std::exception& e ) {
12641274 cancelPendingByAsyncId (async_id);
1265- throw ;
1275+ return makeReadyFuture (Result<std::string, std::string>:: failure (e. what ())) ;
12661276 }
12671277
12681278 return fut;
0 commit comments