57 err.
thread_id = std::this_thread::get_id();
58 err.
timestamp = std::chrono::steady_clock::now();
73 [[nodiscard]]
auto what() const -> std::
string
82 catch (std::exception
const& e)
88 return "Unknown exception";
90 return "No exception";
153 std::lock_guard<std::mutex> lock(mutex_);
154 size_t const id = next_callback_id_++;
155 callbacks_.emplace(
id, std::move(callback));
167 std::lock_guard<std::mutex> lock(mutex_);
168 return callbacks_.erase(
id) > 0;
176 std::lock_guard<std::mutex> lock(mutex_);
177 return callbacks_.count(
id) > 0;
188 std::lock_guard<std::mutex> lock(mutex_);
203 std::lock_guard<std::mutex> lock(mutex_);
206 for (
auto const& [
id, callback] : callbacks_)
228 std::lock_guard<std::mutex> lock(mutex_);
237 std::lock_guard<std::mutex> lock(mutex_);
242 mutable std::mutex mutex_;
243 std::map<size_t, ErrorCallback> callbacks_;
244 size_t next_callback_id_{0};
245 size_t error_count_{0};
266template <
typename Func>
270 ErrorHandledTask(Func&& func, std::shared_ptr<ErrorHandler> handler, std::string description =
"")
271 : func_(std::forward<Func>(func)), handler_(std::move(handler)), description_(std::move(description))
290 std::shared_ptr<ErrorHandler> handler_;
291 std::string description_;
303template <
typename Func>
333 : future_(std::move(future)), error_callback_(nullptr), has_callback_(false)
354 error_callback_ = std::move(callback);
355 has_callback_ =
true;
372 if constexpr (std::is_void_v<T>)
375 return future_.get();
379 if (has_callback_ && error_callback_)
381 error_callback_(std::current_exception());
404 template <
typename Rep,
typename Period>
405 auto wait_for(std::chrono::duration<Rep, Period>
const& timeout_duration)
const
407 return future_.wait_for(timeout_duration);
417 template <
typename Clock,
typename Duration>
418 auto wait_until(std::chrono::time_point<Clock, Duration>
const& timeout_time)
const
420 return future_.wait_until(timeout_time);
428 [[nodiscard]]
auto valid() const ->
bool
430 return future_.valid();
434 std::future<T> future_;
435 std::function<void(std::exception_ptr)> error_callback_;
436 bool has_callback_{
false};
Callable wrapper that catches exceptions and routes them to an ErrorHandler.
ErrorHandledTask(Func &&func, std::shared_ptr< ErrorHandler > handler, std::string description="")
Central registry and dispatcher for task-error callbacks.
auto remove_callback(size_t id) -> bool
Remove a single callback by its ID.
auto has_callback(size_t id) const -> bool
Check whether a callback with the given ID is registered.
void reset_error_count()
Reset the cumulative error count to zero.
void handle_error(TaskError const &error)
Dispatch an error to all registered callbacks.
void clear_callbacks()
Remove all registered error callbacks.
auto error_count() const -> size_t
Return the total number of errors handled since the last reset.
auto add_callback(ErrorCallback callback) -> size_t
Register an error callback.
auto on_error(std::function< void(std::exception_ptr)> callback) -> FutureWithErrorHandler &
Attach an error callback.
auto wait_until(std::chrono::time_point< Clock, Duration > const &timeout_time) const
Block until the result is ready or the given time point is reached.
auto operator=(FutureWithErrorHandler const &) -> FutureWithErrorHandler &=delete
auto get() -> T
Retrieve the result, invoking the error callback on failure.
auto valid() const -> bool
Check whether the future refers to a shared state.
auto operator=(FutureWithErrorHandler &&) -> FutureWithErrorHandler &=default
FutureWithErrorHandler(std::future< T > future)
FutureWithErrorHandler(FutureWithErrorHandler &&)=default
auto wait_for(std::chrono::duration< Rep, Period > const &timeout_duration) const
Block until the result is ready or the timeout elapses.
FutureWithErrorHandler(FutureWithErrorHandler const &)=delete
void wait() const
Block until the result is ready.
auto make_error_handled_task(Func &&func, std::shared_ptr< ErrorHandler > handler, std::string description="")
Factory function that creates an ErrorHandledTask with perfect forwarding.
std::function< void(TaskError const &)> ErrorCallback
Signature for error-handling callbacks registered with ErrorHandler.
Holds diagnostic information captured from a failed task.
std::string task_description
Optional human-readable label supplied when the task was submitted.
void rethrow() const
Re-throw the original exception.
static auto capture(std::string description={}) -> TaskError
Capture the current in-flight exception into a TaskError.
std::exception_ptr exception
The captured exception. Never null when produced by the library.
auto what() const -> std::string
Extract the message string from the stored exception.
std::thread::id thread_id
Id of the thread on which the exception was thrown.
std::chrono::steady_clock::time_point timestamp
Monotonic timestamp recorded immediately after the exception was caught.