Icarus
Vehicle Simulation as a Transformable Computational Graph, built on Vulcan and Janus
Loading...
Searching...
No Matches
ErrorHandler.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <icarus/core/Error.hpp>
13
14#include <cstdlib>
15#include <functional>
16#include <map>
17#include <optional>
18
19namespace icarus {
20
30
34using ErrorCallback = std::function<ErrorPolicy(const SimulationError &)>;
35
45 public:
46 explicit ErrorHandler(LogService &log_service) : log_service_(log_service) {
47 // Set default policies
52 }
53
54 // === Policy Configuration ===
55
57 void SetPolicy(Severity severity, ErrorPolicy policy) { policies_[severity] = policy; }
58
60 void SetCallback(ErrorCallback callback) { callback_ = std::move(callback); }
61
63 void SetMaxErrors(std::size_t max_errors) { max_errors_ = max_errors; }
64
65 // === Error Reporting ===
66
70 // Log the error
71 LogLevel level = SeverityToLogLevel(error.severity);
72 log_service_.Log(level, error.time, "[" + error.component + "] " + error.message);
73
74 // Track counts
75 ++error_counts_[error.severity];
76
77 // Store fatal error
78 if (error.severity == Severity::FATAL) {
79 fatal_error_ = error;
80 }
81
82 // Get policy
83 ErrorPolicy policy = policies_[error.severity];
84
85 // Allow callback to override
86 if (callback_) {
87 policy = callback_(error);
88 }
89
90 // Check max errors
91 if (GetTotalErrorCount() >= max_errors_) {
92 policy = ErrorPolicy::Abort;
93 }
94
95 return policy;
96 }
97
99 ErrorPolicy Report(const Error &exception, double sim_time = 0.0,
100 const std::string &component = "") {
101 return Report(exception.toSimulationError(sim_time, component));
102 }
103
105 [[noreturn]] void ReportFatal(const std::string &message, double sim_time = 0.0) {
106 SimulationError error;
108 error.message = message;
109 error.component = "system";
110 error.time = sim_time;
111 Report(error);
112
113 // Force flush logs before terminating
114 log_service_.FlushAndClear();
115
116 std::abort();
117 }
118
119 // === Query ===
120
122 [[nodiscard]] std::size_t GetErrorCount(Severity severity) const {
123 auto it = error_counts_.find(severity);
124 return it != error_counts_.end() ? it->second : 0;
125 }
126
128 [[nodiscard]] std::size_t GetTotalErrorCount() const {
129 std::size_t total = 0;
130 for (const auto &[severity, count] : error_counts_) {
131 if (severity >= Severity::WARNING) {
132 total += count;
133 }
134 }
135 return total;
136 }
137
139 [[nodiscard]] bool ShouldAbort() const {
140 return fatal_error_.has_value() || GetTotalErrorCount() >= max_errors_;
141 }
142
144 [[nodiscard]] std::optional<SimulationError> GetFatalError() const { return fatal_error_; }
145
146 // === Integration with Simulator ===
147
151 [[nodiscard]] bool ProcessBufferedErrors() {
152 // Errors are already counted in Log(), so just check thresholds
153 return ShouldAbort();
154 }
155
157 void Reset() {
158 error_counts_.clear();
159 fatal_error_.reset();
160 }
161
162 private:
163 LogService &log_service_;
164 ErrorCallback callback_;
165
166 std::map<Severity, ErrorPolicy> policies_;
167 std::map<Severity, std::size_t> error_counts_;
168 std::optional<SimulationError> fatal_error_;
169 std::size_t max_errors_ = 100;
170
171 [[nodiscard]] static LogLevel SeverityToLogLevel(Severity severity) {
172 switch (severity) {
173 case Severity::INFO:
174 return LogLevel::Info;
176 return LogLevel::Warning;
177 case Severity::ERROR:
178 return LogLevel::Error;
179 case Severity::FATAL:
180 return LogLevel::Fatal;
181 }
182 return LogLevel::Error;
183 }
184};
185
190 public:
191 ErrorScope(ErrorHandler &handler, double sim_time, std::string component)
192 : handler_(handler), sim_time_(sim_time), component_(std::move(component)) {}
193
194 ~ErrorScope() = default;
195
197 template <typename F> auto Execute(F &&func) -> decltype(func()) {
198 try {
199 return func();
200 } catch (const Error &e) {
201 handler_.Report(e, sim_time_, component_);
202 throw; // Re-throw after reporting
203 }
204 }
205
206 private:
207 ErrorHandler &handler_;
208 double sim_time_;
209 std::string component_;
210};
211
212} // namespace icarus
Consolidated error handling for Icarus.
Unified logging service for Icarus.
Central error handler integrating logging and exceptions.
Definition ErrorHandler.hpp:44
void SetMaxErrors(std::size_t max_errors)
Set maximum errors before automatic abort.
Definition ErrorHandler.hpp:63
void Reset()
Clear error counts (for new run).
Definition ErrorHandler.hpp:157
ErrorPolicy Report(const SimulationError &error)
Definition ErrorHandler.hpp:69
void SetPolicy(Severity severity, ErrorPolicy policy)
Set default policy for each severity level.
Definition ErrorHandler.hpp:57
void SetCallback(ErrorCallback callback)
Set callback for error handling (can override default policy).
Definition ErrorHandler.hpp:60
bool ProcessBufferedErrors()
Definition ErrorHandler.hpp:151
std::size_t GetErrorCount(Severity severity) const
Get count of errors at each severity.
Definition ErrorHandler.hpp:122
ErrorPolicy Report(const Error &exception, double sim_time=0.0, const std::string &component="")
Report an exception, extracting info automatically.
Definition ErrorHandler.hpp:99
void ReportFatal(const std::string &message, double sim_time=0.0)
Report a fatal error and trigger crash.
Definition ErrorHandler.hpp:105
ErrorHandler(LogService &log_service)
Definition ErrorHandler.hpp:46
std::size_t GetTotalErrorCount() const
Get total error count (WARNING and above).
Definition ErrorHandler.hpp:128
bool ShouldAbort() const
Check if simulation should abort.
Definition ErrorHandler.hpp:139
std::optional< SimulationError > GetFatalError() const
Get the first fatal error (if any).
Definition ErrorHandler.hpp:144
auto Execute(F &&func) -> decltype(func())
Execute a callable, catching and reporting exceptions.
Definition ErrorHandler.hpp:197
ErrorScope(ErrorHandler &handler, double sim_time, std::string component)
Definition ErrorHandler.hpp:191
~ErrorScope()=default
Base class for all Icarus exceptions.
Definition Error.hpp:52
SimulationError toSimulationError(double time=0.0, const std::string &component="") const
Convert to SimulationError for ErrorHandler integration.
Definition Error.hpp:63
Unified logging service for Icarus.
Definition LogService.hpp:289
Definition AggregationTypes.hpp:13
std::function< ErrorPolicy(const SimulationError &)> ErrorCallback
Error handler callback signature.
Definition ErrorHandler.hpp:34
ErrorPolicy
Error handling policy.
Definition ErrorHandler.hpp:24
@ Pause
Pause simulation, allow resume.
Definition ErrorHandler.hpp:26
@ Crash
Immediate termination (for fatal errors).
Definition ErrorHandler.hpp:28
@ Abort
Clean shutdown with debrief.
Definition ErrorHandler.hpp:27
@ Continue
Log and continue (for warnings).
Definition ErrorHandler.hpp:25
Severity
Definition Error.hpp:22
@ WARNING
Warning (may trigger graceful degradation).
Definition Error.hpp:24
@ FATAL
Fatal (simulation must stop).
Definition Error.hpp:26
@ INFO
Informational (logged, no action).
Definition Error.hpp:23
@ ERROR
Error (simulation may continue with fallback).
Definition Error.hpp:25
LogLevel SeverityToLogLevel(Severity severity)
Convert error severity to log level.
Definition ErrorLogging.hpp:19
LogLevel
Log severity levels.
Definition Console.hpp:35
@ Warning
Potential issues.
Definition Console.hpp:40
@ Info
Normal operation.
Definition Console.hpp:38
@ Fatal
Unrecoverable errors.
Definition Console.hpp:42
@ Error
Recoverable errors.
Definition Console.hpp:41
Definition Error.hpp:33
Severity severity
Definition Error.hpp:34
std::string message
Definition Error.hpp:35
std::string component
Definition Error.hpp:36
double time
Definition Error.hpp:37