From 484c710eab5830967ab2bd4ec70cbbaeabd5ab02 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Sat, 11 May 2024 20:46:10 +0530 Subject: [PATCH] GroupKV:Add GetValue which throws exception --- common/groupkv.hpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/common/groupkv.hpp b/common/groupkv.hpp index e6a94f3c4..62fe6d3ee 100644 --- a/common/groupkv.hpp +++ b/common/groupkv.hpp @@ -132,18 +132,30 @@ public: } template - SupportedDataType get_value(const std::string &group, const MultiPart &keyParts, const SupportedDataType &defaultValue, const std::string &callerName="") { + SupportedDataType get_value(const std::string &group, const MultiPart &keyParts) { auto key = joiner(keyParts); auto gm = gkv[group]; if (gm.find(key) == gm.end()) { - LDBUG_LN("WARN:GKV:%s_%s:%s:%s:%s[default]", __func__, callerName.c_str(), group.c_str(), key.c_str(), to_str(defaultValue).c_str()); - return defaultValue; + std::stringstream ss; + ss << "WARN:GKV:" << __func__ << ":" << group << ":Key [" << key << "] not found"; + throw std::range_error(ss.str()); } auto value = gm[key]; - LDBUG_LN("DBUG:GKV:%s_%s:%s:%s:%s", __func__, callerName.c_str(), group.c_str(), key.c_str(), to_str(value).c_str()); return std::get(value); } + template + SupportedDataType get_value(const std::string &group, const MultiPart &keyParts, const SupportedDataType &defaultValue, const std::string &callerName="") { + try { + auto value = get_value(group, keyParts); + LDBUG_LN("DBUG:GKV:%s_%s:%s:%s:%s", __func__, callerName.c_str(), group.c_str(), to_str(keyParts).c_str(), to_str(value).c_str()); + return value; + } catch (std::exception &e) { + } + LDBUG_LN("WARN:GKV:%s_%s:%s:%s:%s[default]", __func__, callerName.c_str(), group.c_str(), to_str(keyParts).c_str(), to_str(defaultValue).c_str()); + return defaultValue; + } + template std::vector get_vector(const std::string &group, const MultiPart &keyParts, const std::vector &defaultValue, const std::string &callerName="") { auto key = joiner(keyParts);