Skip to content

Commit

Permalink
ENH: Handle DataStructure::makePath errors
Browse files Browse the repository at this point in the history
Removes created DataPaths if DataStructure::makePath causes an error.

Fixes #127
  • Loading branch information
mmarineBlueQuartz authored Jul 6, 2023
1 parent 02ede3c commit 672f569
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/complex/DataStructure/DataStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ bool DataStructure::containsData(const DataPath& path) const

Result<LinkedPath> DataStructure::makePath(const DataPath& path)
{
std::vector<DataObject::IdType> createdIds;

try
{
std::vector<DataObject::IdType> pathIds;
Expand All @@ -167,17 +169,24 @@ Result<LinkedPath> DataStructure::makePath(const DataPath& path)
if(data == nullptr)
{
data = complex::DataGroup::Create(*this, name);
createdIds.push_back(data->getId());
}
const BaseGroup* parent = dynamic_cast<const BaseGroup*>(data);
pathIds.push_back(data->getId());

for(usize i = 1; i < path.getLength(); i++)
{
if(parent == nullptr)
{
return complex::MakeErrorResult<LinkedPath>(-3, fmt::format("Target parent object '{}' in path '{}' is not derived from BaseGroup.", name, path.toString()));
}

name = path[i];
data = (*parent)[name];
if(data == nullptr)
{
data = DataGroup::Create(*this, name, pathIds.back());
createdIds.push_back(data->getId());
}
pathIds.push_back(data->getId());

Expand All @@ -187,7 +196,12 @@ Result<LinkedPath> DataStructure::makePath(const DataPath& path)
return {LinkedPath(this, pathIds)};
} catch(const std::exception& e)
{
return complex::MakeErrorResult<LinkedPath>(-2, "Exception thrown when attempting to create a path in the DataStructure");
for(const auto& id : createdIds)
{
removeData(id);
}

return complex::MakeErrorResult<LinkedPath>(-2, fmt::format("Exception thrown when attempting to create a path '{}' in the DataStructure: '{}'", path.toString(), e.what()));
}
}

Expand Down

0 comments on commit 672f569

Please sign in to comment.