Rerun C++ SDK
Loading...
Searching...
No Matches
error.hpp
1#pragma once
2
3#include <cstdint>
4#include <string>
5
6#ifdef __cpp_exceptions
7#include <stdexcept>
8#endif
9
10namespace arrow {
11 class Status;
12}
13
14struct rr_error;
15
16/// Return error if a given rerun::Error producing expression is not rerun::ErrorCode::Ok.
17#define RR_RETURN_NOT_OK(status_expr) \
18 do { \
19 const rerun::Error _status_ = status_expr; \
20 if (_status_.is_err()) { \
21 return _status_; \
22 } \
23 } while (false)
24
25namespace rerun {
26 /// Status codes returned by the SDK as part of `Status`.
27 ///
28 /// Category codes are used to group errors together, but are never returned directly.
29 // ⚠️ Remember to also update `enum CErrorCode` AND `uint32_t rr_error_code` !
30 enum class ErrorCode : uint32_t {
31 Ok = 0x0000'0000,
32 OutOfMemory,
33 NotImplemented,
34 SdkVersionMismatch,
35
36 // Invalid argument errors.
37 _CategoryArgument = 0x0000'0010,
38 UnexpectedNullArgument,
39 InvalidStringArgument,
40 InvalidEnumValue,
41 InvalidRecordingStreamHandle,
42 InvalidSocketAddress,
43 InvalidComponentTypeHandle,
44 InvalidTimeArgument,
45 InvalidTensorDimension,
46 InvalidComponent,
47 InvalidServerUrl = 0x0000'0001a,
48 FileRead,
49 InvalidMemoryLimit,
50
51 // Recording stream errors
52 _CategoryRecordingStream = 0x0000'0100,
53 RecordingStreamRuntimeFailure,
54 RecordingStreamCreationFailure,
55 RecordingStreamSaveFailure,
56 RecordingStreamStdoutFailure,
57 RecordingStreamSpawnFailure,
58 RecordingStreamChunkValidationFailure,
59 RecordingStreamServeGrpcFailure,
60 RecordingStreamFlushTimeout,
61 RecordingStreamFlushFailure,
62
63 // Arrow data processing errors.
64 _CategoryArrow = 0x0000'1000,
65 ArrowFfiSchemaImportError,
66 ArrowFfiArrayImportError,
67
68 // Utility errors.
69 _CategoryUtilities = 0x0001'0000,
70 VideoLoadError,
71
72 // Errors relating to file IO.
73 _CategoryFileIO = 0x0010'0000,
74 FileOpenFailure,
75
76 // Errors directly translated from arrow::StatusCode.
77 _CategoryArrowCppStatus = 0x1000'0000,
78 ArrowStatusCode_KeyError,
79 ArrowStatusCode_TypeError,
80 ArrowStatusCode_Invalid,
81 ArrowStatusCode_IOError,
82 ArrowStatusCode_CapacityError,
83 ArrowStatusCode_IndexError,
84 ArrowStatusCode_Cancelled,
85 ArrowStatusCode_UnknownError,
86 ArrowStatusCode_NotImplemented,
87 ArrowStatusCode_SerializationError,
88 ArrowStatusCode_RError,
89 ArrowStatusCode_CodeGenError,
90 ArrowStatusCode_ExpressionValidationError,
91 ArrowStatusCode_ExecutionError,
92 ArrowStatusCode_AlreadyExists,
93
94 Unknown = 0xFFFF'FFFF,
95 };
96
97 /// Callback function type for log handlers.
98 using StatusLogHandler = void (*)(const class Error& status, void* userdata);
99
100 /// Status outcome object (success or error) returned for fallible operations.
101 ///
102 /// Converts to `true` for success, `false` for failure.
103 class [[nodiscard]] Error {
104 public:
105 /// Result code for the given operation.
106 ErrorCode code = ErrorCode::Ok;
107
108 /// Human readable description of the error.
109 std::string description;
110
111 public:
112 Error() = default;
113
114 Error(ErrorCode _code, std::string _description)
115 : code(_code), description(std::move(_description)) {}
116
117 /// Construct from a C status object.
118 Error(const rr_error& status);
119
120 /// Construct from an arrow status.
121 Error(const arrow::Status& status);
122
123 /// Creates a new error set to ok.
124 static Error ok() {
125 return Error();
126 }
127
128 /// Compare two errors for equality. Requires the description to match.
129 bool operator==(const Error& other) const {
130 return code == other.code && description == other.description;
131 }
132
133 /// Returns true if the code is `Ok`.
134 bool is_ok() const {
135 return code == ErrorCode::Ok;
136 }
137
138 /// Returns true if the code is not `Ok`.
139 bool is_err() const {
140 return code != ErrorCode::Ok;
141 }
142
143 /// Sets global log handler called for `handle`.
144 ///
145 /// The default will log to stderr, unless `RERUN_STRICT` is set to something truthy.
146 ///
147 /// \param handler The handler to call, or `nullptr` to reset to the default.
148 /// \param userdata Userdata pointer that will be passed to each invocation of the handler.
149 ///
150 /// @see log, log_on_failure
151 static void set_log_handler(StatusLogHandler handler, void* userdata = nullptr);
152
153 /// Handle this error based on the set log handler.
154 ///
155 /// If there is no error, nothing happens.
156 ///
157 /// If you have set a log handler with `set_log_handler`, it will be called.
158 /// Else if the `RERUN_STRICT` env-var is set to something truthy,
159 /// an exception will be thrown (if `__cpp_exceptions` are enabled),
160 /// or the program will abort.
161 ///
162 /// If no log handler is installed, and we are not in strict mode,
163 /// the error will be logged to stderr.
164 void handle() const;
165
166 /// Calls the `handle` method and then exits the application with code 1 if the error is not `Ok`.
167 /// @see throw_on_failure
168 void exit_on_failure() const;
169
170 /// Throws a `std::runtime_error` if the status is not `Ok`.
171 ///
172 /// If exceptions are disabled, this will forward to `exit_on_failure` instead.
173 /// @see exit_on_failure
174 void throw_on_failure() const {
175#ifdef __cpp_exceptions
176 if (is_err()) {
177 throw std::runtime_error(description);
178 }
179#else
180 exit_on_failure();
181#endif
182 }
183 };
184} // namespace rerun
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:103
void handle() const
Handle this error based on the set log handler.
bool is_err() const
Returns true if the code is not Ok.
Definition error.hpp:139
bool is_ok() const
Returns true if the code is Ok.
Definition error.hpp:134
static Error ok()
Creates a new error set to ok.
Definition error.hpp:124
static void set_log_handler(StatusLogHandler handler, void *userdata=nullptr)
Sets global log handler called for handle.
void throw_on_failure() const
Throws a std::runtime_error if the status is not Ok.
Definition error.hpp:174
Error(const rr_error &status)
Construct from a C status object.
bool operator==(const Error &other) const
Compare two errors for equality. Requires the description to match.
Definition error.hpp:129
ErrorCode code
Result code for the given operation.
Definition error.hpp:106
Error(const arrow::Status &status)
Construct from an arrow status.
void exit_on_failure() const
Calls the handle method and then exits the application with code 1 if the error is not Ok.
std::string description
Human readable description of the error.
Definition error.hpp:109
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:23
void(*)(const class Error &status, void *userdata) StatusLogHandler
Callback function type for log handlers.
Definition error.hpp:98
ErrorCode
Status codes returned by the SDK as part of Status.
Definition error.hpp:30