From 02d75d02ea21ca552335f3091aeb7e97cfc93238 Mon Sep 17 00:00:00 2001 From: Florian OMNES Date: Thu, 19 Dec 2024 11:45:34 +0100 Subject: [PATCH] Remove function getErrorMessage, use std::optional instead --- .../short-term-storage/AdditionalConstraint.h | 13 +++---------- .../short-term-storage/AdditionalConstraint.cpp | 17 ++++++----------- .../parts/short-term-storage/container.cpp | 8 +++++--- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/libs/antares/study/include/antares/study/parts/short-term-storage/AdditionalConstraint.h b/src/libs/antares/study/include/antares/study/parts/short-term-storage/AdditionalConstraint.h index 8373de0be2..155ace32b1 100644 --- a/src/libs/antares/study/include/antares/study/parts/short-term-storage/AdditionalConstraint.h +++ b/src/libs/antares/study/include/antares/study/parts/short-term-storage/AdditionalConstraint.h @@ -20,6 +20,7 @@ */ #pragma once +#include #include #include @@ -37,19 +38,11 @@ struct AdditionalConstraint unsigned int globalIndex = 0; - bool validate() const; + std::optional validate() const; +private: bool isValidVariable() const; - bool isValidOperatorType() const; - bool isValidHoursRange() const; - - mutable std::string error_message = ""; - - std::string getErrorMessage() const - { - return error_message; - } }; } // namespace Antares::Data::ShortTermStorage diff --git a/src/libs/antares/study/parts/short-term-storage/AdditionalConstraint.cpp b/src/libs/antares/study/parts/short-term-storage/AdditionalConstraint.cpp index c0f3972a78..e8ae87e9e9 100644 --- a/src/libs/antares/study/parts/short-term-storage/AdditionalConstraint.cpp +++ b/src/libs/antares/study/parts/short-term-storage/AdditionalConstraint.cpp @@ -22,34 +22,29 @@ namespace Antares::Data::ShortTermStorage { -bool AdditionalConstraint::validate() const +std::optional AdditionalConstraint::validate() const { if (cluster_id.empty()) { - error_message = "Cluster ID is empty."; - return false; + return "Cluster ID is empty."; } if (!isValidVariable()) { - error_message = "Invalid variable type. Must be 'injection', 'withdrawal', or 'netting'."; - return false; + return "Invalid variable type. Must be 'injection', 'withdrawal', or 'netting'."; } if (!isValidOperatorType()) { - error_message = "Invalid operator type. Must be 'less', 'equal', or 'greater'."; - return false; + return "Invalid operator type. Must be 'less', 'equal', or 'greater'."; } if (!isValidHoursRange()) { - error_message = "Hours set contains invalid values. Must be between 1 and 168."; - return false; + return "Hours set contains invalid values. Must be between 1 and 168."; } - error_message.clear(); - return true; + return {}; } bool AdditionalConstraint::isValidHoursRange() const diff --git a/src/libs/antares/study/parts/short-term-storage/container.cpp b/src/libs/antares/study/parts/short-term-storage/container.cpp index db2bbbf070..b0a6cfb9fe 100644 --- a/src/libs/antares/study/parts/short-term-storage/container.cpp +++ b/src/libs/antares/study/parts/short-term-storage/container.cpp @@ -125,10 +125,11 @@ bool STStorageInput::LoadConstraintsFromIniFile(const fs::path& parent_path) } } - if (!constraint.validate()) + if (auto errorMaybe = constraint.validate()) { - throw std::runtime_error("Invalid constraint in section: " + section->name + "\n" - + constraint.getErrorMessage()); + logs.error() << "Invalid constraint in section: " << section->name; + logs.error() << errorMaybe.value(); + return false; } auto it = std::find_if(storagesByIndex.begin(), @@ -140,6 +141,7 @@ bool STStorageInput::LoadConstraintsFromIniFile(const fs::path& parent_path) logs.warning() << " from file " << pathIni; logs.warning() << "Constraint " << section->name << " does not reference an existing cluster"; + return false; } else {