Skip to content

Commit

Permalink
split try catch for system and libraries loading
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Dec 31, 2024
1 parent 6df9b65 commit 9341362
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 20 deletions.
6 changes: 0 additions & 6 deletions src/solver/modeler/loadFiles/handleErrors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,4 @@ void handleYamlError(const YAML::Exception& e, const std::string& context)
logs.error() << e.what();
}

void handleRuntimeError(const std::runtime_error& e, const std::string& context)
{
logs.error() << "Error while parsing or converting the file: " << context;
logs.error() << e.what();
}

} // namespace Antares::Solver::LoadFiles
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ Study::SystemModel::System loadSystem(const std::filesystem::path& studyPath,

void handleYamlError(const YAML::Exception& e, const std::string& context);

void handleRuntimeError(const std::runtime_error& e, const std::string& context);

/// Generic error class for all loading errors to catch in the main
class ErrorLoadingYaml: public std::runtime_error
{
Expand Down
25 changes: 20 additions & 5 deletions src/solver/modeler/loadFiles/readLibraries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,37 @@ namespace Antares::Solver::LoadFiles

static Study::SystemModel::Library loadSingleLibrary(const fs::path& filePath)
{
std::string libraryStr;
try
{
const std::string libraryStr = IO::readFile(filePath);
ModelParser::Parser parser;
ModelParser::Library libraryObj = parser.parse(libraryStr);
libraryStr = IO::readFile(filePath);
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while trying to read this library file: " << filePath;
throw ErrorLoadingYaml(e.what());
}

return ModelConverter::convert(libraryObj);
ModelParser::Parser parser;
ModelParser::Library libraryObj;

try
{
libraryObj = parser.parse(libraryStr);
}
catch (const YAML::Exception& e)
{
handleYamlError(e, filePath.string());
throw ErrorLoadingYaml(e.what());
}

try
{
return ModelConverter::convert(libraryObj);
}
catch (const std::runtime_error& e)
{
handleRuntimeError(e, filePath.string());
logs.error() << "Error while converting this library yaml: "<< filePath;
throw ErrorLoadingYaml(e.what());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/solver/modeler/loadFiles/readParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ModelerParameters loadParameters(const fs::path& studyPath)
}
catch (const std::runtime_error& e)
{
handleRuntimeError(e, filename);
logs.error() << "Error while trying to read file parameters.yml";
throw ErrorLoadingYaml(e.what());
}

Expand Down
24 changes: 19 additions & 5 deletions src/solver/modeler/loadFiles/readSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,36 @@ Study::SystemModel::System loadSystem(const fs::path& studyPath,
const std::vector<Study::SystemModel::Library>& libraries)
{
std::string filename = "system.yml";
std::string systemStr;
try
{
const std::string systemStr = IO::readFile(studyPath / "input" / filename);
SystemParser::Parser parser;
SystemParser::System systemObj = parser.parse(systemStr);
systemStr = IO::readFile(studyPath / "input" / filename);
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while trying to read file system.yml";
throw ErrorLoadingYaml(e.what());
}

return SystemConverter::convert(systemObj, libraries);
SystemParser::Parser parser;
SystemParser::System systemObj;
try
{
systemObj = parser.parse(systemStr);
}
catch (const YAML::Exception& e)
{
handleYamlError(e, filename);
throw ErrorLoadingYaml(e.what());
}

try
{
return SystemConverter::convert(systemObj, libraries);
}
catch (const std::runtime_error& e)
{
handleRuntimeError(e, filename);
logs.error() << "Error while converting the system yaml to components";
throw ErrorLoadingYaml(e.what());
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/tests/src/solver/modeler/loadFiles/testLoadModelerFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#include "files-system.h"

BOOST_AUTO_TEST_SUITE(read_modeler_parameters)
BOOST_AUTO_TEST_SUITE(test_modeler_files_loading)

namespace fs = std::filesystem;

Expand All @@ -52,6 +52,15 @@ struct FixtureLoadFile
}
};

BOOST_AUTO_TEST_CASE(files_not_existing)
{
fs::path studyPath = CREATE_TMP_DIR_BASED_ON_TEST_NAME();
std::vector<Antares::Study::SystemModel::Library> libraries;

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

BOOST_FIXTURE_TEST_CASE(read_one_lib_treile, FixtureLoadFile)
{
std::ofstream libStream(libraryDirPath / "simple.yml");
Expand Down
6 changes: 6 additions & 0 deletions src/tests/src/solver/modeler/loadFiles/testParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ BOOST_AUTO_TEST_CASE(parameters_missing)

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

BOOST_AUTO_TEST_CASE(file_missing)
{
auto studyPath = CREATE_TMP_DIR_BASED_ON_TEST_NAME();
BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadParameters(studyPath), std::runtime_error);
}

0 comments on commit 9341362

Please sign in to comment.