From e9c17581010f148f00c5bee1126c49406df0cbde Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 4 Mar 2026 21:42:30 +0100 Subject: [PATCH] feat: add thespian_unexpected to C API --- include/thespian/c/instance.h | 1 + src/c/instance.cpp | 12 ++++++++++++ test/ip_tcp_client_server_c.cpp | 8 ++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/thespian/c/instance.h b/include/thespian/c/instance.h index 8e48239..09d3bd8 100644 --- a/include/thespian/c/instance.h +++ b/include/thespian/c/instance.h @@ -17,6 +17,7 @@ void thespian_receive(thespian_receiver, thespian_behaviour_state, thespian_receiver_dtor); thespian_result thespian_exit(const char *status); +thespian_result thespian_unexpected(cbor_buffer msg); bool thespian_get_trap(); bool thespian_set_trap(bool); diff --git a/src/c/instance.cpp b/src/c/instance.cpp index a8b058b..7b25aec 100644 --- a/src/c/instance.cpp +++ b/src/c/instance.cpp @@ -47,6 +47,18 @@ auto thespian_exit(const char *status) -> thespian_result { return &exit_error_buf; } +auto thespian_unexpected(cbor_buffer m) -> thespian_result { + cbor::buffer buf; + const uint8_t *data = m.base; + std::copy(data, data + m.len, back_inserter(buf)); // NOLINT + auto r = thespian::unexpected(buf); + if (r) + return nullptr; + exit_msg_buf = r.error(); + exit_error_buf = {exit_msg_buf.data(), exit_msg_buf.size()}; + return &exit_error_buf; +} + void thespian_link(thespian_handle h) { thespian::handle *h_{ reinterpret_cast( // NOLINT(*-reinterpret-cast) diff --git a/test/ip_tcp_client_server_c.cpp b/test/ip_tcp_client_server_c.cpp index 548d749..6f53142 100644 --- a/test/ip_tcp_client_server_c.cpp +++ b/test/ip_tcp_client_server_c.cpp @@ -79,7 +79,7 @@ static thespian_result cc_receive(thespian_behaviour_state s, thespian_handle, send(st->client, cbor::array("client_connection", "done")); return thespian_exit("normal"); } else { - LOG("cc_receive: UNEXPECTED msg=%s\n", msg_json); + return thespian_unexpected(m); } return nullptr; } @@ -131,7 +131,7 @@ static thespian_result client_receive(thespian_behaviour_state s, send(st->server, cbor::array("client", "done")); return thespian_exit("normal"); } else { - LOG("client_receive: UNEXPECTED msg=%s\n", msg_json); + return thespian_unexpected(m); } return nullptr; } @@ -184,7 +184,7 @@ static thespian_result sc_receive(thespian_behaviour_state s, thespian_handle, send(st->server, cbor::array("server_connection", "done")); return thespian_exit("normal"); } else { - LOG("sc_receive: UNEXPECTED msg=%s\n", msg_json); + return thespian_unexpected(m); } return nullptr; } @@ -245,7 +245,7 @@ static thespian_result server_receive(thespian_behaviour_state s, st->acceptor_closed, st->client_done); st->server_conn_done = true; } else { - LOG("server_receive: UNEXPECTED msg=%s\n", msg_json); + return thespian_unexpected(m); } if (st->acceptor_closed && st->client_done && st->server_conn_done) { LOG("server_receive: all done, exiting success\n");