ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
task_error.hpp
Go to the documentation of this file.
1#pragma once
2
8#include <chrono>
9#include <exception>
10#include <functional>
11#include <string>
12#include <thread>
13#include <utility>
14
15namespace threadschedule
16{
17
22{
24 std::exception_ptr exception;
26 std::string task_description;
28 std::thread::id std_id;
30 std::chrono::steady_clock::time_point timestamp;
31
36 [[nodiscard]] static auto
37 capture(std::string description = {}) -> task_error
38 {
39 return { std::current_exception(), std::move(description), std::this_thread::get_id(),
40 std::chrono::steady_clock::now() };
41 }
42
47 [[nodiscard]] auto
48 what() const -> std::string
49 {
50 try
51 {
52 if (exception)
53 std::rethrow_exception(exception);
54 }
55 catch (std::exception const& error)
56 {
57 return error.what();
58 }
59 catch (...)
60 {
61 return "Unknown exception";
62 }
63 return "No exception";
64 }
65
69 void
70 rethrow() const
71 {
72 if (exception)
73 std::rethrow_exception(exception);
74 }
75};
76
78using error_callback = std::function<void(task_error const&)>;
79
80} // namespace threadschedule
std::function< void(task_error const &)> error_callback
Callback signature for asynchronous task error reporting.
Captured information about a task failure.
std::chrono::steady_clock::time_point timestamp
Capture timestamp using steady_clock.
std::string task_description
Optional textual description of the failing task.
auto what() const -> std::string
Return a printable error message if exception derives from std::exception.
static auto capture(std::string description={}) -> task_error
Capture failure context for the current exception state.
std::exception_ptr exception
Original exception object captured from the task.
void rethrow() const
Rethrow captured exception if present.
std::thread::id std_id
std::thread id where the failure occurred.