diff --git a/src/complex/DataStructure/DataStructure.cpp b/src/complex/DataStructure/DataStructure.cpp index 9c4a3e409d..41f1ed0af5 100644 --- a/src/complex/DataStructure/DataStructure.cpp +++ b/src/complex/DataStructure/DataStructure.cpp @@ -159,6 +159,8 @@ bool DataStructure::containsData(const DataPath& path) const Result DataStructure::makePath(const DataPath& path) { + std::vector createdIds; + try { std::vector pathIds; @@ -167,17 +169,24 @@ Result DataStructure::makePath(const DataPath& path) if(data == nullptr) { data = complex::DataGroup::Create(*this, name); + createdIds.push_back(data->getId()); } const BaseGroup* parent = dynamic_cast(data); pathIds.push_back(data->getId()); for(usize i = 1; i < path.getLength(); i++) { + if(parent == nullptr) + { + return complex::MakeErrorResult(-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()); @@ -187,7 +196,12 @@ Result DataStructure::makePath(const DataPath& path) return {LinkedPath(this, pathIds)}; } catch(const std::exception& e) { - return complex::MakeErrorResult(-2, "Exception thrown when attempting to create a path in the DataStructure"); + for(const auto& id : createdIds) + { + removeData(id); + } + + return complex::MakeErrorResult(-2, fmt::format("Exception thrown when attempting to create a path '{}' in the DataStructure: '{}'", path.toString(), e.what())); } }