From 44df6de06878c36dcc70d62b0df49c98fbcb9df3 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 17 Dec 2024 11:50:53 +0100 Subject: [PATCH] Implement NotFoundError method for improved error handling in Registry and RegistryItem --- kratos/includes/registry.h | 11 +++++++++++ kratos/includes/registry_item.h | 16 ++++++++++------ kratos/sources/registry.cpp | 30 +++++++++++++++++++++--------- kratos/sources/registry_item.cpp | 8 ++++---- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/kratos/includes/registry.h b/kratos/includes/registry.h index 2201940c8aaf..ccae39c7e24f 100644 --- a/kratos/includes/registry.h +++ b/kratos/includes/registry.h @@ -216,6 +216,17 @@ class KRATOS_API(KRATOS_CORE) Registry final ///@name Private Operations ///@{ + /** + * @brief This method throws an error message for a not found item. + * @param rFullName The full name of the item. + * @param rItemName The name of the item. + * @param pCurrentItem The current item. + */ + static void NotFoundError( + const std::string& rFullName, + const std::string& rItemName, + RegistryItem* pCurrentItem + ); ///@} ///@name Private Access diff --git a/kratos/includes/registry_item.h b/kratos/includes/registry_item.h index 77e366db78b7..d69defdaeb22 100644 --- a/kratos/includes/registry_item.h +++ b/kratos/includes/registry_item.h @@ -295,6 +295,16 @@ class KRATOS_API(KRATOS_CORE) RegistryItem return true; } + ///@} + ///@name Operations + ///@{ + + /** + * @brief Returns the sub items name list available in the registry item (for error messaged). + * @return std::vector The sub items name list. + */ + std::vector GetSubItemAvailableList() const; + ///@} ///@name Input and output ///@{ @@ -340,12 +350,6 @@ class KRATOS_API(KRATOS_CORE) RegistryItem return buffer.str(); } - /** - * @brief Returns the sub items name list available in the registry item (for error messaged). - * @return std::vector The sub items name list. - */ - std::vector GetSubItemAvailableList() const; - /** * @brief This method throws an error message for a not found item. * @param rItemName The name of the item. diff --git a/kratos/sources/registry.cpp b/kratos/sources/registry.cpp index 96bf2491fc6c..fedfe74dcdf0 100644 --- a/kratos/sources/registry.cpp +++ b/kratos/sources/registry.cpp @@ -43,9 +43,8 @@ namespace auto& r_item_name = item_path[i]; if(p_current_item->HasItem(r_item_name)){ p_current_item = &p_current_item->GetItem(r_item_name); - } - else{ - KRATOS_ERROR << "The item \"" << rItemFullName << "\" is not found in the registry. The item \"" << p_current_item->Name() << "\" does not have \"" << r_item_name << "\"" << std::endl; + } else { + NotFoundError(rItemFullName, r_item_name, p_current_item); } } @@ -65,18 +64,16 @@ namespace auto& r_item_name = item_path[i]; if(p_current_item->HasItem(r_item_name)){ p_current_item = &p_current_item->GetItem(r_item_name); - } - else{ - KRATOS_ERROR << "The item \"" << rItemFullName << "\" is not found in the registry. The item \"" << p_current_item->Name() << "\" does not have \"" << r_item_name << "\"" << std::endl; + } else { + NotFoundError(rItemFullName, r_item_name, p_current_item); } } auto& r_item_name = item_path.back(); if(p_current_item->HasItem(r_item_name)){ p_current_item->RemoveItem(r_item_name); - } - else{ - KRATOS_ERROR << "The item \"" << rItemFullName << "\" is not found in the registry. The item \"" << p_current_item->Name() << "\" does not have \"" << r_item_name << "\"" << std::endl; + } else { + NotFoundError(rItemFullName, r_item_name, p_current_item); } } @@ -158,6 +155,21 @@ namespace return GetRootRegistryItem().ToJson(Indentation); } + void Registry::NotFoundError( + const std::string& rFullName, + const std::string& rItemName, + RegistryItem* pCurrentItem + ) + { + const std::vector available_list = pCurrentItem->GetSubItemAvailableList(); + std::stringstream error_message_buffer; + error_message_buffer << "The item \"" << rFullName << "\" is not found in the registry. The item \"" << pCurrentItem->Name() << "\" does not have \"" << rItemName << "\". The available objects are: \n"; + for (std::string const& item : available_list) { + error_message_buffer << "\t\t" << item << "\n"; + } + KRATOS_ERROR << error_message_buffer.str() << std::endl; + } + RegistryItem& Registry::GetRootRegistryItem() { if (!mspRootRegistryItem) { diff --git a/kratos/sources/registry_item.cpp b/kratos/sources/registry_item.cpp index d7489acaabdf..53ac62a37de7 100644 --- a/kratos/sources/registry_item.cpp +++ b/kratos/sources/registry_item.cpp @@ -106,12 +106,12 @@ namespace Kratos void RegistryItem::NotFoundError(const std::string& rItemName) const { const std::vector available_list = GetSubItemAvailableList(); - std::stringstream available_list_str; - available_list_str << "The RegistryItem " << this->Name() << " does not have an item with name " << rItemName << ". The available objects are: \n"; + std::stringstream error_message_buffer; + error_message_buffer << "The RegistryItem " << this->Name() << " does not have an item with name " << rItemName << ". The available objects are: \n"; for (std::string const& item : available_list) { - available_list_str << "\t\t" << item << "\n"; + error_message_buffer << "\t\t" << item << "\n"; } - KRATOS_ERROR << available_list_str.str() << std::endl; + KRATOS_ERROR << error_message_buffer.str() << std::endl; } std::string RegistryItem::GetValueString() const