Skip to content

Commit

Permalink
error handling for system
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Dec 20, 2024
1 parent d429081 commit f8c3d70
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/solver/modeler/loadFiles/readLibraries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static Study::SystemModel::Library loadSingleLibrary(const fs::path& filePath)
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while parsing or converting this yaml file:";
logs.error() << "Error while parsing or converting this library yaml file:";
logs.error() << filePath;
logs.error() << e.what();

Expand Down
17 changes: 13 additions & 4 deletions src/solver/modeler/loadFiles/readSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include <antares/io/file.h>
#include <antares/logs/logs.h>
#include <antares/solver/systemParser/converter.h>
#include <antares/solver/systemParser/parser.h>
#include "antares/solver/modeler/loadFiles/loadFiles.h"
Expand All @@ -33,12 +34,20 @@ Study::SystemModel::System loadSystem(const fs::path& studyPath,
const std::vector<Study::SystemModel::Library>& libraries)
{
const std::string systemStr = IO::readFile(studyPath / "input" / "system.yml");

SystemParser::Parser parser;
// Add try/catch and error handling
SystemParser::System systemObj = parser.parse(systemStr);

return SystemConverter::convert(systemObj, libraries);
try
{
SystemParser::System systemObj = parser.parse(systemStr);
return SystemConverter::convert(systemObj, libraries);
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while parsing or converting the system file:";
logs.error() << e.what();

throw std::runtime_error(e.what());
}
}

} // namespace Antares::Solver::LoadFiles
26 changes: 26 additions & 0 deletions src/tests/src/solver/modeler/loadFiles/testLoadModelerFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,30 @@ BOOST_FIXTURE_TEST_CASE(read_system_file, FixtureLoadFile)
auto system = Antares::Solver::LoadFiles::loadSystem(studyPath, libraries);
}

BOOST_FIXTURE_TEST_CASE(read_invalid_system_file, FixtureLoadFile)
{
std::ofstream libStream(libraryDirPath / "simple.yml");
libStream << R"(
library:
id: std
description: lib_description
port-types: []
models:
- id: generator
description: A basic generator model
)";
libStream.close();

std::ofstream systemStream(inputPath / "system.yml");
systemStream << R"(
system:
)";
systemStream.close();

auto libraries = Antares::Solver::LoadFiles::loadLibraries(studyPath);
BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadSystem(studyPath, libraries),
std::runtime_error);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit f8c3d70

Please sign in to comment.