Skip to content

Commit

Permalink
Implement NotFoundError method for improved error handling in Registr…
Browse files Browse the repository at this point in the history
…y and RegistryItem
  • Loading branch information
loumalouomega committed Dec 17, 2024
1 parent 7cef84e commit 44df6de
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
11 changes: 11 additions & 0 deletions kratos/includes/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions kratos/includes/registry_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> The sub items name list.
*/
std::vector<std::string> GetSubItemAvailableList() const;

///@}
///@name Input and output
///@{
Expand Down Expand Up @@ -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<std::string> The sub items name list.
*/
std::vector<std::string> GetSubItemAvailableList() const;

/**
* @brief This method throws an error message for a not found item.
* @param rItemName The name of the item.
Expand Down
30 changes: 21 additions & 9 deletions kratos/sources/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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<std::string> 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) {
Expand Down
8 changes: 4 additions & 4 deletions kratos/sources/registry_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ namespace Kratos
void RegistryItem::NotFoundError(const std::string& rItemName) const
{
const std::vector<std::string> 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
Expand Down

0 comments on commit 44df6de

Please sign in to comment.