Skip to content

Commit

Permalink
add error handling functions to avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Dec 30, 2024
1 parent 511dc7e commit 00183c4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/solver/modeler/loadFiles/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(SOURCES
readSystem.cpp
readLibraries.cpp
readParameters.cpp
loadFiles.cpp

include/antares/solver/modeler/loadFiles/loadFiles.h
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <filesystem>
#include <vector>

#include <yaml-cpp/yaml.h>

#include <antares/solver/modeler/parameters/modelerParameters.h>
#include <antares/study/system-model/library.h>
#include <antares/study/system-model/system.h>
Expand All @@ -38,6 +40,10 @@ 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);

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
44 changes: 44 additions & 0 deletions src/solver/modeler/loadFiles/loadFiles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2007-2024, RTE (https://www.rte-france.com)
* See AUTHORS.txt
* SPDX-License-Identifier: MPL-2.0
* This file is part of Antares-Simulator,
* Adequacy and Performance assessment for interconnected energy networks.
*
* Antares_Simulator is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public Licence 2.0 as published by
* the Mozilla Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Antares_Simulator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public Licence 2.0 for more details.
*
* You should have received a copy of the Mozilla Public Licence 2.0
* along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/

#include <antares/logs/logs.h>
#include "antares/solver/modeler/loadFiles/loadFiles.h"

namespace Antares::Solver::LoadFiles
{

void handleYamlError(const YAML::Exception& e, const std::string& context)
{
logs.error() << "Error while parsing the yaml file: " << context;
if (!e.mark.is_null())
{
logs.error() << "Line " << e.mark.line << " column " << e.mark.column;
}
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
15 changes: 4 additions & 11 deletions src/solver/modeler/loadFiles/readParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,20 @@ namespace Antares::Solver::LoadFiles

ModelerParameters loadParameters(const fs::path& studyPath)
{
std::string filename = "parameters.yml";
try
{
const std::string paramStr = IO::readFile(studyPath / "parameters.yml");
const std::string paramStr = IO::readFile(studyPath / filename);
return parseModelerParameters(paramStr);
}
catch (const YAML::Exception& e)
{
logs.error() << "Error while parsing the yaml parameters file";
if (!e.mark.is_null())
{
logs.error() << "Line " << e.mark.line << " column " << e.mark.column;
}
logs.error() << e.what();

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

handleRuntimeError(e, filename);
throw ErrorLoadingYaml(e.what());
}
}
Expand Down

0 comments on commit 00183c4

Please sign in to comment.