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