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