Skip to content

Commit

Permalink
[API] Throw exception instead of cerr log (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed May 7, 2024
1 parent dae7ab6 commit 0e8a951
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/cpp/benders/benders_core/SimulationOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
#include <filesystem>

#include "LogUtils.h"
Json::Value get_value_from_json(const std::filesystem::path &file_name) {
Json::Value SimulationOptions::get_value_from_json(
const std::filesystem::path &file_name) {
Json::Value _input;
std::ifstream input_file_l(file_name, std::ifstream::binary);
Json::CharReaderBuilder builder_l;
std::string errs;
if (!parseFromStream(builder_l, input_file_l, &_input, &errs)) {
std::cerr << LOGLOCATION << "Invalid options file: " << file_name << "\n"
<< errs;
std::exit(1);
using namespace std::string_literals;
auto message = LOGLOCATION + "Invalid options file: "s + file_name.string()
+ "\n" + errs;
throw InvalidOptionFileException(message);
}
return _input;
}
Expand Down Expand Up @@ -171,6 +173,10 @@ BendersBaseOptions SimulationOptions::get_benders_options() const {

return result;
}
SimulationOptions::InvalidOptionFileException::InvalidOptionFileException(
const std::string &arg)
: runtime_error(arg) {}


ExternalLoopOptions SimulationOptions::GetExternalLoopOptions() const {
return {EXT_LOOP_CRITERION_VALUE, EXT_LOOP_CRITERION_TOLERANCE,
Expand Down
6 changes: 6 additions & 0 deletions src/cpp/benders/benders_core/include/SimulationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ class SimulationOptions {

private:
void set_weights();
Json::Value get_value_from_json(const std::filesystem::path &file_name);

class InvalidOptionFileException : public std::runtime_error {
public:
explicit InvalidOptionFileException(const std::string &arg);
};
};

0 comments on commit 0e8a951

Please sign in to comment.