Skip to content

Commit

Permalink
catch specific error in main
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Dec 24, 2024
1 parent b39407f commit 663b786
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ std::vector<Study::SystemModel::Library> loadLibraries(const std::filesystem::pa
Study::SystemModel::System loadSystem(const std::filesystem::path& studyPath,
const std::vector<Study::SystemModel::Library>& libraries);

/// Generic error class for all loading errors to catch in the main
class ErrorLoadingYaml: public std::runtime_error
{
public:
explicit ErrorLoadingYaml(const std::string& s):
runtime_error(s)
{
}
};

} // namespace Antares::Solver::LoadFiles
10 changes: 5 additions & 5 deletions src/solver/modeler/loadFiles/readLibraries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ namespace Antares::Solver::LoadFiles

static Study::SystemModel::Library loadSingleLibrary(const fs::path& filePath)
{
const std::string libraryStr = IO::readFile(filePath);

ModelParser::Parser parser;
try
{
const std::string libraryStr = IO::readFile(filePath);
ModelParser::Parser parser;
ModelParser::Library libraryObj = parser.parse(libraryStr);

return ModelConverter::convert(libraryObj);
}
catch (const YAML::Exception& e)
Expand All @@ -51,15 +51,15 @@ static Study::SystemModel::Library loadSingleLibrary(const fs::path& filePath)
}
logs.error() << e.what();

throw std::runtime_error(e.what());
throw ErrorLoadingYaml(e.what());
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while parsing or converting this library yaml file:";
logs.error() << filePath;
logs.error() << e.what();

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

Expand Down
4 changes: 2 additions & 2 deletions src/solver/modeler/loadFiles/readParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ ModelerParameters loadParameters(const fs::path& studyPath)
}
logs.error() << e.what();

throw std::runtime_error(e.what());
throw ErrorLoadingYaml(e.what());
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while parsing the yaml parameters file:";
logs.error() << e.what();

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

Expand Down
10 changes: 5 additions & 5 deletions src/solver/modeler/loadFiles/readSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ namespace Antares::Solver::LoadFiles
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;

try
{
const std::string systemStr = IO::readFile(studyPath / "input" / "system.yml");
SystemParser::Parser parser;
SystemParser::System systemObj = parser.parse(systemStr);

return SystemConverter::convert(systemObj, libraries);
}
catch (const YAML::Exception& e)
Expand All @@ -52,14 +52,14 @@ Study::SystemModel::System loadSystem(const fs::path& studyPath,
}
logs.error() << e.what();

throw std::runtime_error(e.what());
throw ErrorLoadingYaml(e.what());
}
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());
throw ErrorLoadingYaml(e.what());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/solver/modeler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, const char** argv)
const auto libraries = LoadFiles::loadLibraries(studyPath);
const auto system = LoadFiles::loadSystem(studyPath, libraries);
}
catch (...)
catch (const LoadFiles::ErrorLoadingYaml&)
{
logs.error() << "Error while loading files, exiting";
return EXIT_FAILURE;
Expand Down

0 comments on commit 663b786

Please sign in to comment.