From 672f56928ae73ec1d9cadf6f67ffbc9e7e06b19c Mon Sep 17 00:00:00 2001 From: Matthew Marine Date: Thu, 6 Jul 2023 15:27:42 -0400 Subject: [PATCH] ENH: Handle DataStructure::makePath errors Removes created DataPaths if DataStructure::makePath causes an error. Fixes #127 --- src/complex/DataStructure/DataStructure.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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())); } }