Skip to content

Commit

Permalink
Remove getErrorMessage, use dedicated struct (#2542)
Browse files Browse the repository at this point in the history
  • Loading branch information
flomnes authored Dec 19, 2024
1 parent f7878b0 commit 9a81f67
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,17 @@ struct AdditionalConstraint

unsigned int globalIndex = 0;

bool validate() const;
struct ValidateResult
{
bool ok;
std::string error_msg;
};

bool isValidVariable() const;
ValidateResult 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,29 @@

namespace Antares::Data::ShortTermStorage
{
bool AdditionalConstraint::validate() const
AdditionalConstraint::ValidateResult AdditionalConstraint::validate() const
{
if (cluster_id.empty())
{
error_message = "Cluster ID is empty.";
return false;
return {false, "Cluster ID is empty."};
}

if (!isValidVariable())
{
error_message = "Invalid variable type. Must be 'injection', 'withdrawal', or 'netting'.";
return false;
return {false, "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 {false, "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 {false, "Hours set contains invalid values. Must be between 1 and 168."};
}

error_message.clear();
return true;
return {true, ""};
}

bool AdditionalConstraint::isValidHoursRange() const
Expand Down
8 changes: 5 additions & 3 deletions src/libs/antares/study/parts/short-term-storage/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ bool STStorageInput::LoadConstraintsFromIniFile(const fs::path& parent_path)
}
}

if (!constraint.validate())
if (auto ret = constraint.validate(); !ret.ok)
{
throw std::runtime_error("Invalid constraint in section: " + section->name + "\n"
+ constraint.getErrorMessage());
logs.error() << "Invalid constraint in section: " << section->name;
logs.error() << ret.error_msg;
return false;
}

auto it = std::find_if(storagesByIndex.begin(),
Expand All @@ -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
{
Expand Down

0 comments on commit 9a81f67

Please sign in to comment.