From b98d604cd37df790dd5f91804fc9d7d516b4f9f6 Mon Sep 17 00:00:00 2001
From: Giovanna Lazzari Miotto <giovanna.lazzari.miotto@cern.ch>
Date: Thu, 8 Feb 2024 18:36:42 +0100
Subject: [PATCH] except: Handle parsing errors

---
 src/config.cc | 45 +++++++++++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 6 deletions(-)

diff --git a/src/config.cc b/src/config.cc
index 7e598ff3..45db7d23 100644
--- a/src/config.cc
+++ b/src/config.cc
@@ -17,17 +17,50 @@ bool ends_with(const std::string &str, const std::string &suffix) {
 
 template <>
 uint32_t ConfigMap::Get<uint32_t>(const std::string &k) {
-  return get(k).asUInt();
+  ValueType value = get(k);
+  try {
+    return value.asUInt();
+  } catch (std::exception &e) {
+    LOG(DEBUG) << "Value '" << value.asString() << "' not natively convertible to uint32";
+    try {
+      return static_cast<uint32_t>(std::stoul(value.asString()));
+    } catch (std::exception &e) {
+      LOG(ERROR) << "Configuration error with key: '" << k << "'";
+      throw;
+    }
+  }
 }
 
 template <>
 uint64_t ConfigMap::Get<uint64_t>(const std::string &k) {
-  return get(k).asUInt64();
+  ValueType value = get(k);
+  try {
+    return value.asUInt64();
+  } catch (std::exception &e) {
+    LOG(DEBUG) << "Value '" << value.asString() << "' not natively convertible to uint64";
+    try {
+      return static_cast<uint64_t>(std::stoul(value.asString()));
+    } catch (std::exception &e) {
+      LOG(ERROR) << "Configuration error with key: '" << k << "'";
+      throw;
+    }
+  }
 }
 
 template <>
 int ConfigMap::Get<int>(const std::string &k) {
-  return get(k).asInt();
+  ValueType value = get(k);
+  try {
+    return value.asInt();
+  } catch (std::exception &e) {
+    LOG(DEBUG) << "Value '" << value.asString() << "' not natively convertible to integer";
+    try {
+      return std::stoi(value.asString());
+    } catch (std::exception &e) {
+      LOG(ERROR) << "Configuration error with key: '" << k << "'";
+      throw;
+    }
+  }
 }
 
 template <>
@@ -128,8 +161,8 @@ Config::Config(const std::string &filename) {
   dma_device_ = dict_.at("dma_dev");
   attempt_tcp_reconnect_ = dict_.Get<bool>("dev_TCPAutoReconnectOnFailure");
 
-  source_id_ = dict_.Get<uint32_t>("sourceID");
-  port_ = dict_.Get<uint32_t>("port"),
+  //  source_id_ = dict_.Get<uint32_t>("sourceID");
+  port_ = dict_.Get<int>("port"),
 
   num_threads_ = dict_.Get<uint32_t>("threads");
   max_file_size_ = dict_.Get<uint64_t>("max_file_size");
@@ -147,7 +180,7 @@ Config::Config(const std::string &filename) {
   num_orbits_per_file_ = dict_.Get<uint64_t>("nOrbitsPerFile");
   force_write_out_ = dict_.Get<bool>("output_force_write");
 
-  processor_type_ = ToProcessorType(dict_.Get("processorType"));
+  processor_type_ = ToProcessorType(dict_.Get("processor_type"));
   enable_processing_ = dict_.Get<bool>("enable_stream_processor");
   apply_zero_suppression_ = dict_.Get<bool>("doZS");
 }
-- 
GitLab