src.ctf.lttng-live: make queries report errors with exceptions
[deliverable/babeltrace.git] / src / plugins / ctf / lttng-live / lttng-live.cpp
index 91ce3f9d75fc6c6b24935f71180a519fa4c601cf..6d0b82e082e0542c986eedd1b5c1eda988ec8a92 100644 (file)
@@ -29,6 +29,7 @@
 #include "plugins/common/muxing/muxing.h"
 #include "plugins/common/param-validation/param-validation.h"
 #include "cpp-common/cfg-logging-error-reporting.hpp"
+#include "cpp-common/cfg-logging-error-reporting-throw.hpp"
 
 #include "data-stream.hpp"
 #include "metadata.hpp"
@@ -1740,12 +1741,9 @@ static struct bt_param_validation_map_value_entry_descr list_sessions_params[] =
      bt_param_validation_value_descr::makeString()},
     BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END};
 
-static bt_component_class_query_method_status
-lttng_live_query_list_sessions(bt2::ConstMapValue params,
-                               nonstd::optional<bt2::Value::Shared>& result,
-                               const bt2_common::LogCfg& logCfg)
+static bt2::Value::Shared lttng_live_query_list_sessions(bt2::ConstMapValue params,
+                                                         const bt2_common::LogCfg& logCfg)
 {
-    bt_component_class_query_method_status status;
     const char *url;
     live_viewer_connection::UP viewer_connection;
     enum lttng_live_viewer_status viewer_status;
@@ -1755,12 +1753,10 @@ lttng_live_query_list_sessions(bt2::ConstMapValue params,
     validation_status =
         bt_param_validation_validate(params.libObjPtr(), list_sessions_params, &validate_error);
     if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
-        status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
-        goto error;
+        throw bt2_common::MemoryError {};
     } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
-        status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
-        BT_CLOGE_APPEND_CAUSE("%s", validate_error);
-        goto error;
+        bt2_common::GCharUP errorFreer {validate_error};
+        BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2::Error, "%s", validate_error);
     }
 
     url = params[URL_PARAM]->asString().value().c_str();
@@ -1768,43 +1764,21 @@ lttng_live_query_list_sessions(bt2::ConstMapValue params,
     viewer_status = live_viewer_connection_create(url, true, NULL, logCfg, viewer_connection);
     if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
         if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
-            BT_CLOGE_APPEND_CAUSE("Failed to create viewer connection");
-            status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
+            BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2::Error, "Failed to create viewer connection");
         } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) {
-            status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN;
+            throw bt2_common::TryAgain {};
         } else {
             bt_common_abort();
         }
-        goto error;
-    }
-
-    status = live_viewer_connection_list_sessions(viewer_connection.get(), result);
-    if (status != BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK) {
-        BT_CLOGE_APPEND_CAUSE("Failed to list viewer sessions");
-        goto error;
     }
 
-    goto end;
-
-error:
-    if (status >= 0) {
-        status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
-    }
-
-end:
-    g_free(validate_error);
-
-    return status;
+    return live_viewer_connection_list_sessions(viewer_connection.get());
 }
 
-static bt_component_class_query_method_status
-lttng_live_query_support_info(bt2::ConstMapValue params,
-                              nonstd::optional<bt2::Value::Shared>& result,
-                              const bt2_common::LogCfg& logCfg)
+static bt2::Value::Shared lttng_live_query_support_info(bt2::ConstMapValue params,
+                                                        const bt2_common::LogCfg& logCfg)
 {
-    bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
     nonstd::optional<bt2::ConstValue> inputValue;
-    double weight = 0;
     struct bt_common_lttng_live_url_parts parts = {0};
     bt_common_lttng_live_url_parts_deleter partsDeleter {parts};
 
@@ -1813,29 +1787,25 @@ lttng_live_query_support_info(bt2::ConstMapValue params,
 
     nonstd::optional<bt2::ConstValue> typeValue = params["type"];
     if (!typeValue) {
-        BT_CLOGE_APPEND_CAUSE("Missing expected `type` parameter.");
-        goto error;
+        BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2::Error, "Missing expected `type` parameter.");
     }
 
     if (!typeValue->isString()) {
-        BT_CLOGE_APPEND_CAUSE("`type` parameter is not a string value.");
-        goto error;
+        BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2::Error, "`type` parameter is not a string value.");
     }
 
     if (typeValue->asString().value() != "string") {
         /* We don't handle file system paths */
-        goto create_result;
+        return bt2::RealValue::create();
     }
 
     inputValue = params["input"];
     if (!inputValue) {
-        BT_CLOGE_APPEND_CAUSE("Missing expected `input` parameter.");
-        goto error;
+        BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2::Error, "Missing expected `input` parameter.");
     }
 
     if (!inputValue->isString()) {
-        BT_CLOGE_APPEND_CAUSE("`input` parameter is not a string value.");
-        goto error;
+        BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2::Error, "`input` parameter is not a string value.");
     }
 
     parts = bt_common_parse_lttng_live_url(inputValue->asString().value().c_str(), NULL, 0);
@@ -1844,22 +1814,10 @@ lttng_live_query_support_info(bt2::ConstMapValue params,
          * Looks pretty much like an LTTng live URL: we got the
          * session name part, which forms a complete URL.
          */
-        weight = .75;
-    }
-
-create_result:
-    result = bt2::RealValue::create(weight);
-    goto end;
-
-error:
-    if (status >= 0) {
-        status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
+        return bt2::RealValue::create(.75);
     }
 
-    BT_ASSERT(!result);
-
-end:
-    return status;
+    return bt2::RealValue::create();
 }
 
 BT_HIDDEN
@@ -1876,24 +1834,23 @@ bt_component_class_query_method_status lttng_live_query(bt_self_component_class_
     bt2_common::LogCfg logCfg(log_level, *self_comp_class);
 
     try {
-        bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
         bt2::ConstMapValue paramsObj(params);
         nonstd::optional<bt2::Value::Shared> resultObj;
 
         if (strcmp(object, "sessions") == 0) {
-            status = lttng_live_query_list_sessions(paramsObj, resultObj, logCfg);
+            resultObj = lttng_live_query_list_sessions(paramsObj, logCfg);
         } else if (strcmp(object, "babeltrace.support-info") == 0) {
-            status = lttng_live_query_support_info(paramsObj, resultObj, logCfg);
+            resultObj = lttng_live_query_support_info(paramsObj, logCfg);
         } else {
             BT_CLOGI("Unknown query object `%s`", object);
             return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT;
         }
 
-        if (status == BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK) {
-            *result = resultObj->release().libObjPtr();
-        }
+        *result = resultObj->release().libObjPtr();
 
-        return status;
+        return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
+    } catch (const bt2_common::TryAgain&) {
+        return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
     } catch (const std::bad_alloc&) {
         return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
     } catch (const bt2_common::Error&) {
This page took 0.027282 seconds and 5 git commands to generate.