41
41
#include < cloe/core.hpp> // for logger::get
42
42
#include < fable/utility.hpp> // for read_conf
43
43
44
+ #include " main_stack.hpp" // for Stack, new_stack
44
45
#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
47
47
48
48
namespace engine {
49
49
@@ -67,6 +67,19 @@ struct RunOptions {
67
67
68
68
Simulation* GLOBAL_SIMULATION_INSTANCE{nullptr };
69
69
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
+
70
83
inline int run (const RunOptions& opt, const std::vector<std::string>& filepaths) {
71
84
cloe::logger::get (" cloe" )->info (" Cloe {}" , CLOE_ENGINE_VERSION);
72
85
cloe::StackOptions stack_opt = opt.stack_options ;
@@ -85,19 +98,14 @@ inline int run(const RunOptions& opt, const std::vector<std::string>& filepaths)
85
98
// Load the stack file:
86
99
cloe::Stack s;
87
100
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
+ });
92
107
} catch (cloe::ConcludedError& e) {
93
108
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;
101
109
}
102
110
103
111
// Create simulation:
@@ -109,7 +117,7 @@ inline int run(const RunOptions& opt, const std::vector<std::string>& filepaths)
109
117
sim.set_report_progress (opt.report_progress );
110
118
111
119
// Run simulation:
112
- auto result = sim.run ();
120
+ auto result = handle_cloe_error (*stack_opt. error , [&]() { return sim.run (); } );
113
121
if (result.outcome == SimulationOutcome::NoStart) {
114
122
// If we didn't get past the initialization phase, don't output any
115
123
// statistics or write any files, just go home.
0 commit comments