Skip to content

Commit

Permalink
rename C++ status types to error
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Aug 17, 2023
1 parent 3c11343 commit e0a839c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion rerun_cpp/src/rerun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
#include "rerun/datatypes.hpp"

// Rerun API.
#include "rerun/error.hpp"
#include "rerun/recording_stream.hpp"
#include "rerun/sdk_info.hpp"
#include "rerun/status.hpp"
10 changes: 5 additions & 5 deletions rerun_cpp/src/rerun/status.cpp → rerun_cpp/src/rerun/error.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "status.hpp"
#include "error.hpp"

#include <rerun.h>

namespace rerun {
static StatusLogHandler global_log_handler = nullptr;
static void* global_log_handler_user_data = nullptr;

Status::Status(const rr_error& status)
: code(static_cast<StatusCode>(status.code)), description(status.description) {}
Error::Error(const rr_error& status)
: code(static_cast<ErrorCode>(status.code)), description(status.description) {}

void Status::set_log_handler(StatusLogHandler handler, void* userdata) {
void Error::set_log_handler(StatusLogHandler handler, void* userdata) {
global_log_handler = handler;
global_log_handler_user_data = userdata;
}

void Status::log() const {
void Error::log() const {
if (global_log_handler) {
global_log_handler(*this, global_log_handler_user_data);
} else {
Expand Down
24 changes: 12 additions & 12 deletions rerun_cpp/src/rerun/status.hpp → rerun_cpp/src/rerun/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace rerun {
/// Status codes returned by the SDK as part of `Status`.
///
/// Category codes are used to group errors together, but are never returned directly.
enum class StatusCode : uint32_t {
enum class ErrorCode : uint32_t {
Ok = 0,

// Invalid argument errors.
Expand All @@ -38,46 +38,46 @@ namespace rerun {
};

/// Callback function type for log handlers.
using StatusLogHandler = void (*)(const class Status& status, void* userdata);
using StatusLogHandler = void (*)(const class Error& status, void* userdata);

/// Status outcome object (success or error) returned for fallible operations.
///
/// Converts to `true` for success, `false` for failure.
class [[nodiscard]] Status {
class [[nodiscard]] Error {
public:
/// Result code for the given operation.
StatusCode code = StatusCode::Ok;
ErrorCode code = ErrorCode::Ok;

/// Human readable description of the error.
std::string description;

public:
Status() = default;
Error() = default;

Status(StatusCode _code, std::string _description)
Error(ErrorCode _code, std::string _description)
: code(_code), description(std::move(_description)) {}

/// Construct from a C status object.
Status(const rr_error& status);
Error(const rr_error& status);

/// Returns true if the code is `Ok`.
bool is_ok() const {
return code == StatusCode::Ok;
return code == ErrorCode::Ok;
}

/// Returns true if the code is not `Ok`.
bool is_err() const {
return code != StatusCode::Ok;
return code != ErrorCode::Ok;
}

/// Sets global log handler called for `log` and `log_error`.
/// Sets global log handler called for `log` and `log_on_failure`.
///
/// The default will log to stderr.
///
/// @param handler The handler to call, or `nullptr` to reset to the default.
/// @param userdata Userdata pointer that will be passed to each invocation of the handler.
///
/// @see log, log_error
/// @see log, log_on_failure
static void set_log_handler(StatusLogHandler handler, void* userdata = nullptr);

/// Logs this status via the global log handler.
Expand All @@ -88,7 +88,7 @@ namespace rerun {
/// Logs this status if failed via the global log handler.
///
/// @see set_log_handler
void log_error() const {
void log_on_failure() const {
if (is_err()) {
log();
}
Expand Down
12 changes: 6 additions & 6 deletions rerun_cpp/src/rerun/recording_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace rerun {

rr_error status = {};
this->_id = rr_recording_stream_new(&store_info, &status);
Status(status).log_error();
Error(status).log_on_failure();
}

RecordingStream::RecordingStream(RecordingStream&& other)
Expand Down Expand Up @@ -75,13 +75,13 @@ namespace rerun {
}
}

Status RecordingStream::connect(const char* tcp_addr, float flush_timeout_sec) {
Error RecordingStream::connect(const char* tcp_addr, float flush_timeout_sec) {
rr_error status = {};
rr_recording_stream_connect(_id, tcp_addr, flush_timeout_sec, &status);
return status;
}

Status RecordingStream::save(const char* path) {
Error RecordingStream::save(const char* path) {
rr_error status = {};
rr_recording_stream_save(_id, path, &status);
return status;
Expand All @@ -91,16 +91,16 @@ namespace rerun {
rr_recording_stream_flush_blocking(_id);
}

Status RecordingStream::try_log_data_row(
Error RecordingStream::try_log_data_row(
const char* entity_path, size_t num_instances, size_t num_data_cells,
const DataCell* data_cells
) {
// Map to C API:
std::vector<rr_data_cell> c_data_cells(num_data_cells);
for (size_t i = 0; i < num_data_cells; i++) {
if (data_cells[i].buffer == nullptr) {
return Status(
StatusCode::UnexpectedNullArgument,
return Error(
ErrorCode::UnexpectedNullArgument,
"DataCell buffer is null for cell " + std::to_string(i)
);
}
Expand Down
22 changes: 11 additions & 11 deletions rerun_cpp/src/rerun/recording_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <vector>

#include "data_cell.hpp"
#include "status.hpp"
#include "error.hpp"

// TODO(#2873): Should avoid leaking arrow headers.
#include <arrow/result.h>
Expand Down Expand Up @@ -102,12 +102,12 @@ namespace rerun {
/// timeout, and can cause a call to `flush` to block indefinitely.
///
/// This function returns immediately.
Status connect(const char* tcp_addr = "127.0.0.1:9876", float flush_timeout_sec = 2.0);
Error connect(const char* tcp_addr = "127.0.0.1:9876", float flush_timeout_sec = 2.0);

/// Stream all log-data to a given file.
///
/// This function returns immediately.
Status save(const char* path);
Error save(const char* path);

/// Initiates a flush the batching pipeline and waits for it to propagate.
///
Expand All @@ -125,7 +125,7 @@ namespace rerun {
/// TODO(andreas): Would be nice if this were able to combine both log_archetype and
/// log_components!
///
/// Logs any failure via `Status::log_error`
/// Logs any failure via `Error::log_on_failure`
template <typename T>
void log(const char* entity_path, const T& archetype) {
log_archetype(entity_path, archetype);
Expand All @@ -135,17 +135,17 @@ namespace rerun {
///
/// Prefer this interface for ease of use over the more general `log_components` interface.
///
/// Logs any failure via `Status::log_error`
/// Logs any failure via `Error::log_on_failure`
template <typename T>
void log_archetype(const char* entity_path, const T& archetype) {
try_log_archetype(entity_path, archetype).log_error();
try_log_archetype(entity_path, archetype).log_on_failure();
}

/// Logs a an archetype, returning an error on failure.
///
/// @see log_archetype
template <typename T>
Status try_log_archetype(const char* entity_path, const T& archetype) {
Error try_log_archetype(const char* entity_path, const T& archetype) {
const auto data_cells = archetype.to_data_cells().ValueOrDie(); // TODO: Error handling.
return try_log_data_row(
entity_path,
Expand All @@ -164,17 +164,17 @@ namespace rerun {
///
/// TODO(andreas): More documentation, examples etc.
///
/// Logs any failure via `Status::log_error`
/// Logs any failure via `Error::log_on_failure`
template <typename... Ts>
void log_components(const char* entity_path, const Ts&... component_array) {
try_log_components(entity_path, component_array...).log_error();
try_log_components(entity_path, component_array...).log_on_failure();
}

/// Logs a list of component arrays, returning an error on failure.
///
/// @see log_components
template <typename... Ts>
Status try_log_components(const char* entity_path, const Ts&... component_array) {
Error try_log_components(const char* entity_path, const Ts&... component_array) {
// TODO(andreas): Handle splats.
const size_t num_instances = size_of_first_collection(component_array...);

Expand All @@ -194,7 +194,7 @@ namespace rerun {
///
/// I.e. logs a number of components arrays (each with a same number of instances) to a
/// single entity path.
Status try_log_data_row(
Error try_log_data_row(
const char* entity_path, size_t num_instances, size_t num_data_cells,
const DataCell* data_cells
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#include <catch2/catch_test_macros.hpp>

#include <rerun/status.hpp>
#include <rerun/error.hpp>

/// Checks if the given operation logs the expected status code.
template <typename Op>
auto check_logged_status(
Op operation, rerun::StatusCode expected_status_code = rerun::StatusCode::Ok
Op operation, rerun::ErrorCode expected_status_code = rerun::ErrorCode::Ok
) {
static rerun::Status last_logged_status;
static rerun::Error last_logged_status;

// Set to Ok since nothing logged indicates success for most methods.
last_logged_status.code = rerun::StatusCode::Ok;
last_logged_status.code = rerun::ErrorCode::Ok;

rerun::Status::set_log_handler(
[](const rerun::Status& status, void* userdata) {
*static_cast<rerun::Status*>(userdata) = status;
rerun::Error::set_log_handler(
[](const rerun::Error& status, void* userdata) {
*static_cast<rerun::Error*>(userdata) = status;
},
&last_logged_status
);

struct CheckOnDestruct {
rerun::StatusCode expected_status_code;
rerun::ErrorCode expected_status_code;

~CheckOnDestruct() {
CHECK(last_logged_status.code == expected_status_code);
if (expected_status_code != rerun::StatusCode::Ok) {
if (expected_status_code != rerun::ErrorCode::Ok) {
CHECK(last_logged_status.description.length() > 0);
} else {
CHECK(last_logged_status.description.length() == 0);
}
rerun::Status::set_log_handler(nullptr);
rerun::Error::set_log_handler(nullptr);
}
} check = {expected_status_code};

Expand Down
28 changes: 14 additions & 14 deletions rerun_cpp/tests/recording_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <arrow/buffer.h>
#pragma GCC diagnostic pop

#include "status_check.hpp"
#include "error_check.hpp"

#include <rerun/archetypes/points2d.hpp>
#include <rerun/components/point2d.hpp>
Expand Down Expand Up @@ -61,15 +61,15 @@ SCENARIO("RecordingStream can be created, destroyed and lists correct properties
THEN("creating a new stream logs a null argument error") {
check_logged_status(
[&] { rr::RecordingStream stream(nullptr, kind); },
rr::StatusCode::UnexpectedNullArgument
rr::ErrorCode::UnexpectedNullArgument
);
}
}
AND_GIVEN("invalid utf8 character sequence for the application id") {
THEN("creating a new stream logs an invalid string argument error") {
check_logged_status(
[&] { rr::RecordingStream stream("\xc3\x28", kind); },
rr::StatusCode::InvalidStringArgument
rr::ErrorCode::InvalidStringArgument
);
}
}
Expand Down Expand Up @@ -193,7 +193,7 @@ SCENARIO("RecordingStream can log to file", TEST_TAG) {

AND_GIVEN("a nullptr for the save path") {
THEN("then the save call returns a null argument error") {
CHECK(stream0->save(nullptr).code == rr::StatusCode::UnexpectedNullArgument);
CHECK(stream0->save(nullptr).code == rr::ErrorCode::UnexpectedNullArgument);
}
}
AND_GIVEN("valid save path " << test_rrd0) {
Expand All @@ -202,7 +202,7 @@ SCENARIO("RecordingStream can log to file", TEST_TAG) {
THEN("then the save call fails") {
CHECK(
stream0->save(test_rrd0.c_str()).code ==
rr::StatusCode::RecordingStreamSaveFailure
rr::ErrorCode::RecordingStreamSaveFailure
);
}
}
Expand Down Expand Up @@ -263,14 +263,14 @@ SCENARIO("RecordingStream can log to file", TEST_TAG) {
void test_logging_to_connection(const char* address, rr::RecordingStream& stream) {
AND_GIVEN("a nullptr for the socket address") {
THEN("then the connect call returns a null argument error") {
CHECK(stream.connect(nullptr, 0.0f).code == rr::StatusCode::UnexpectedNullArgument);
CHECK(stream.connect(nullptr, 0.0f).code == rr::ErrorCode::UnexpectedNullArgument);
}
}
AND_GIVEN("an invalid address for the socket address") {
THEN("then the save call fails") {
CHECK(
stream.connect("definitely not valid!", 0.0f).code ==
rr::StatusCode::InvalidSocketAddress
rr::ErrorCode::InvalidSocketAddress
);
}
}
Expand Down Expand Up @@ -337,11 +337,11 @@ SCENARIO("Recording stream handles invalid logging gracefully", TEST_TAG) {
rr::RecordingStream stream("test");

AND_GIVEN("an invalid path") {
auto variant = GENERATE(table<const char*, rr::StatusCode>({
std::tuple<const char*, rr::StatusCode>("////", rr::StatusCode::InvalidEntityPath),
std::tuple<const char*, rr::StatusCode>(
auto variant = GENERATE(table<const char*, rr::ErrorCode>({
std::tuple<const char*, rr::ErrorCode>("////", rr::ErrorCode::InvalidEntityPath),
std::tuple<const char*, rr::ErrorCode>(
nullptr,
rr::StatusCode::UnexpectedNullArgument
rr::ErrorCode::UnexpectedNullArgument
),
}));
const auto [path, error] = variant;
Expand Down Expand Up @@ -386,7 +386,7 @@ SCENARIO("Recording stream handles invalid logging gracefully", TEST_TAG) {
THEN("try_log_data_row fails with UnexpectedNullArgument") {
CHECK(
stream.try_log_data_row(path, 1, 1, &cell).code ==
rr::StatusCode::UnexpectedNullArgument
rr::ErrorCode::UnexpectedNullArgument
);
}
}
Expand All @@ -399,7 +399,7 @@ SCENARIO("Recording stream handles invalid logging gracefully", TEST_TAG) {
THEN("try_log_data_row fails with UnexpectedNullArgument") {
CHECK(
stream.try_log_data_row(path, 1, 1, &cell).code ==
rr::StatusCode::UnexpectedNullArgument
rr::ErrorCode::UnexpectedNullArgument
);
}
}
Expand All @@ -413,7 +413,7 @@ SCENARIO("Recording stream handles invalid logging gracefully", TEST_TAG) {
THEN("try_log_data_row fails with ArrowIpcMessageParsingFailure") {
CHECK(
stream.try_log_data_row(path, 1, 1, &cell).code ==
rr::StatusCode::ArrowIpcMessageParsingFailure
rr::ErrorCode::ArrowIpcMessageParsingFailure
);
}
}
Expand Down

0 comments on commit e0a839c

Please sign in to comment.