Skip to content

Commit e4c94ca

Browse files
committed
engine: Provide better errors when simulation errors occur
1 parent 5376189 commit e4c94ca

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

engine/src/main_run.hpp

+22-14
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
#include <cloe/core.hpp> // for logger::get
4242
#include <fable/utility.hpp> // for read_conf
4343

44+
#include "main_stack.hpp" // for Stack, new_stack
4445
#include "simulation.hpp" // for Simulation, SimulationResult
45-
#include "main_stack.hpp" // for Stack, new_stack
46-
#include "stack.hpp" // for Stack
46+
#include "stack.hpp" // for Stack
4747

4848
namespace engine {
4949

@@ -67,6 +67,19 @@ struct RunOptions {
6767

6868
Simulation* GLOBAL_SIMULATION_INSTANCE{nullptr};
6969

70+
template <typename Func>
71+
auto handle_cloe_error(std::ostream& out, Func f) -> decltype(f()) {
72+
try {
73+
return f();
74+
} catch (cloe::Error& e) {
75+
out << "Error: " << e.what() << std::endl;
76+
if (e.has_explanation()) {
77+
out << " Note:\n" << fable::indent_string(e.explanation(), " ") << std::endl;
78+
}
79+
throw cloe::ConcludedError(e);
80+
}
81+
}
82+
7083
inline int run(const RunOptions& opt, const std::vector<std::string>& filepaths) {
7184
cloe::logger::get("cloe")->info("Cloe {}", CLOE_ENGINE_VERSION);
7285
cloe::StackOptions stack_opt = opt.stack_options;
@@ -85,19 +98,14 @@ inline int run(const RunOptions& opt, const std::vector<std::string>& filepaths)
8598
// Load the stack file:
8699
cloe::Stack s;
87100
try {
88-
s = cloe::new_stack(stack_opt, filepaths);
89-
if (!opt.allow_empty) {
90-
s.check_completeness();
91-
}
101+
handle_cloe_error(*stack_opt.error, [&]() {
102+
s = cloe::new_stack(stack_opt, filepaths);
103+
if (!opt.allow_empty) {
104+
s.check_completeness();
105+
}
106+
});
92107
} catch (cloe::ConcludedError& e) {
93108
return EXIT_FAILURE;
94-
} catch (cloe::Error& e) {
95-
*stack_opt.error << "Error: " << e.what() << std::endl;
96-
if (e.has_explanation()) {
97-
*stack_opt.error << " Note:\n"
98-
<< fable::indent_string(e.explanation(), " ") << std::endl;
99-
}
100-
return EXIT_FAILURE;
101109
}
102110

103111
// Create simulation:
@@ -109,7 +117,7 @@ inline int run(const RunOptions& opt, const std::vector<std::string>& filepaths)
109117
sim.set_report_progress(opt.report_progress);
110118

111119
// Run simulation:
112-
auto result = sim.run();
120+
auto result = handle_cloe_error(*stack_opt.error, [&]() { return sim.run(); });
113121
if (result.outcome == SimulationOutcome::NoStart) {
114122
// If we didn't get past the initialization phase, don't output any
115123
// statistics or write any files, just go home.

0 commit comments

Comments
 (0)