Skip to content

Commit

Permalink
error handling for libraries loading
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Dec 20, 2024
1 parent 02f366f commit d429081
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/solver/modeler/loadFiles/readLibraries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,20 @@ static Study::SystemModel::Library loadSingleLibrary(const fs::path& filePath)
const std::string libraryStr = IO::readFile(filePath);

ModelParser::Parser parser;
// Add try/catch and error handling
ModelParser::Library libraryObj = parser.parse(libraryStr);
return ModelConverter::convert(libraryObj);

try
{
ModelParser::Library libraryObj = parser.parse(libraryStr);
return ModelConverter::convert(libraryObj);
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while parsing or converting this yaml file:";
logs.error() << filePath;
logs.error() << e.what();

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

std::vector<Study::SystemModel::Library> loadLibraries(const fs::path& studyPath)
Expand Down
13 changes: 13 additions & 0 deletions src/tests/src/solver/modeler/loadFiles/testLoadModelerFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ BOOST_FIXTURE_TEST_CASE(dont_read_bad_extension, FixtureLoadFile)
BOOST_CHECK(libraries.empty());
}

BOOST_FIXTURE_TEST_CASE(incorrect_library, FixtureLoadFile)
{
std::ofstream libStream(libraryDirPath / "simple.yml");
libStream << R"(
library:
port-types: []
models: []
)";
libStream.close();

BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadLibraries(studyPath), std::runtime_error);
}

BOOST_FIXTURE_TEST_CASE(read_several_lib_file, FixtureLoadFile)
{
std::ofstream libStream(libraryDirPath / "simple.yml");
Expand Down

0 comments on commit d429081

Please sign in to comment.