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 FileReadFailure,
76
77 // Errors directly translated from arrow::StatusCode.
78 _CategoryArrowCppStatus = 0x1000'0000,
79 ArrowStatusCode_KeyError,
80 ArrowStatusCode_TypeError,
81 ArrowStatusCode_Invalid,
82 ArrowStatusCode_IOError,
83 ArrowStatusCode_CapacityError,
84 ArrowStatusCode_IndexError,
85 ArrowStatusCode_Cancelled,
86 ArrowStatusCode_UnknownError,
87 ArrowStatusCode_NotImplemented,
88 ArrowStatusCode_SerializationError,
89 ArrowStatusCode_RError,
90 ArrowStatusCode_CodeGenError,
91 ArrowStatusCode_ExpressionValidationError,
92 ArrowStatusCode_ExecutionError,
93 ArrowStatusCode_AlreadyExists,
94
95 Unknown = 0xFFFF'FFFF,
96 };
97
98 /// Callback function type for log handlers.
99 using StatusLogHandler = void (*)(const class Error& status, void* userdata);
100
101 /// Status outcome object (success or error) returned for fallible operations.
102 ///
103 /// Converts to `true` for success, `false` for failure.
104 class [[nodiscard]] Error {
105 public:
106 /// Result code for the given operation.
107 ErrorCode code = ErrorCode::Ok;
108
109 /// Human readable description of the error.
110 std::string description;
111
112 public:
113 Error() = default;
114
115 Error(ErrorCode _code, std::string _description)
116 : code(_code), description(std::move(_description)) {}
117
118 /// Construct from a C status object.
119 Error(const rr_error& status);
120
121 /// Construct from an arrow status.
122 Error(const arrow::Status& status);
123
124 /// Creates a new error set to ok.
125 static Error ok() {
126 return Error();
127 }
128
129 /// Compare two errors for equality. Requires the description to match.
130 bool operator==(const Error& other) const {
131 return code == other.code && description == other.description;
132 }
133
134 /// Returns true if the code is `Ok`.
135 bool is_ok() const {
136 return code == ErrorCode::Ok;
137 }
138
139 /// Returns true if the code is not `Ok`.
140 bool is_err() const {
141 return code != ErrorCode::Ok;
142 }
143
144 /// Sets global log handler called for `handle`.
145 ///
146 /// The default will log to stderr, unless `RERUN_STRICT` is set to something truthy.
147 ///
148 /// \param handler The handler to call, or `nullptr` to reset to the default.
149 /// \param userdata Userdata pointer that will be passed to each invocation of the handler.
150 ///
151 /// @see log, log_on_failure
152 static void set_log_handler(StatusLogHandler handler, void* userdata = nullptr);
153
154 /// Handle this error based on the set log handler.
155 ///
156 /// If there is no error, nothing happens.
157 ///
158 /// If you have set a log handler with `set_log_handler`, it will be called.
159 /// Else if the `RERUN_STRICT` env-var is set to something truthy,
160 /// an exception will be thrown (if `__cpp_exceptions` are enabled),
161 /// or the program will abort.
162 ///
163 /// If no log handler is installed, and we are not in strict mode,
164 /// the error will be logged to stderr.
165 void handle() const;
166
167 /// Calls the `handle` method and then exits the application with code 1 if the error is not `Ok`.
168 /// @see throw_on_failure
169 void exit_on_failure() const;
170
171 /// Throws a `std::runtime_error` if the status is not `Ok`.
172 ///
173 /// If exceptions are disabled, this will forward to `exit_on_failure` instead.
174 /// @see exit_on_failure
175 void throw_on_failure() const {
176#ifdef __cpp_exceptions
177 if (is_err()) {
178 throw std::runtime_error(description);
179 }
180#else
181 exit_on_failure();
182#endif
183 }
184 };
185} // namespace rerun
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:104
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:140
bool is_ok() const
Returns true if the code is Ok.
Definition error.hpp:135
static Error ok()
Creates a new error set to ok.
Definition error.hpp:125
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:175
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:130
ErrorCode code
Result code for the given operation.
Definition error.hpp:107
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:110
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:26
void(*)(const class Error &status, void *userdata) StatusLogHandler
Callback function type for log handlers.
Definition error.hpp:99
ErrorCode
Status codes returned by the SDK as part of Status.
Definition error.hpp:30