Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove function getErrorMessage, use std::optional instead #2541

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#pragma once
#include <optional>
#include <set>
#include <string>

Expand All @@ -37,19 +38,11 @@ struct AdditionalConstraint

unsigned int globalIndex = 0;

bool validate() const;
std::optional<std::string> 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
std::optional<std::string> 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
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 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(),
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
Loading