From 935542b451b5bd834a3bf144cb1c437981874715 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 21 Jul 2024 05:48:55 +0000 Subject: [PATCH] Correct implementation of std::error_code wrapper. --- lib-protocol/source/adapter.hpp | 8 ++++---- {lib-shared => lib-protocol}/source/category.cpp | 12 ++++-------- {lib-shared => lib-protocol}/source/category.hpp | 6 ++++-- lib-protocol/source/connection.hpp | 6 ++---- lib-shared/source/error.hpp | 7 ------- 5 files changed, 14 insertions(+), 25 deletions(-) rename {lib-shared => lib-protocol}/source/category.cpp (86%) rename {lib-shared => lib-protocol}/source/category.hpp (95%) diff --git a/lib-protocol/source/adapter.hpp b/lib-protocol/source/adapter.hpp index 4b5aaf820..8d9018237 100644 --- a/lib-protocol/source/adapter.hpp +++ b/lib-protocol/source/adapter.hpp @@ -60,8 +60,8 @@ class Adapter { U stream_; template - void Convert(Handler_ &&handler, const std::exception_ptr &error) { - boost::asio::post(get_executor(), [handler = std::forward(handler), error = Category::Convert(error)]() mutable { + void Convert(Handler_ &&handler) { + boost::asio::post(get_executor(), [handler = std::forward(handler), error = Category::Convert(std::current_exception())]() mutable { std::move(handler)(error, 0); }); } @@ -98,7 +98,7 @@ class Adapter { return std::move(handler)(eof ? asio::error::eof : boost::system::error_code(), writ); }); } catch (...) { - Convert(std::move(handler), std::current_exception()); + Convert(std::move(handler)); } }, __FUNCTION__); } @@ -118,7 +118,7 @@ class Adapter { return std::move(handler)(boost::system::error_code(), writ); }); } catch (...) { - Convert(std::move(handler), std::current_exception()); + Convert(std::move(handler)); } }, __FUNCTION__); } diff --git a/lib-shared/source/category.cpp b/lib-protocol/source/category.cpp similarity index 86% rename from lib-shared/source/category.cpp rename to lib-protocol/source/category.cpp index 72268f1ae..320a94dcf 100644 --- a/lib-shared/source/category.cpp +++ b/lib-protocol/source/category.cpp @@ -38,19 +38,15 @@ namespace { std::string Category::message(int index) const { try { - std::rethrow_exception(Convert(index)); + const std::unique_lock lock(mutex_); + if (auto error = errors_.extract(index)) + std::rethrow_exception(error.mapped()); + orc_insist(false); } catch (const std::exception &error) { return error.what(); } } -std::exception_ptr Category::Convert(int index) noexcept { - const std::unique_lock lock(mutex_); - if (const auto error = errors_.extract(index)) - return error.mapped(); - orc_insist(false); -} - boost::system::error_code Category::Convert(const std::exception_ptr &error) noexcept { const std::unique_lock lock(mutex_); // XXX: clang-tidy might need fixing, as this just bans ?: diff --git a/lib-shared/source/category.hpp b/lib-protocol/source/category.hpp similarity index 95% rename from lib-shared/source/category.hpp rename to lib-protocol/source/category.hpp index aab79aef6..91aec8766 100644 --- a/lib-shared/source/category.hpp +++ b/lib-protocol/source/category.hpp @@ -37,8 +37,6 @@ class Category : } std::string message(int index) const override; - - static std::exception_ptr Convert(int index) noexcept; static boost::system::error_code Convert(const std::exception_ptr &error) noexcept; }; @@ -53,4 +51,8 @@ constexpr const boost::system::error_category &orchid_category() noexcept { return orc::Categories::category_; } +#define orc_adapt(error) do { \ + orc_throw(error.what()); \ +} while (false) + #endif//ORCHID_CATEGORY_HPP diff --git a/lib-protocol/source/connection.hpp b/lib-protocol/source/connection.hpp index b57453ab0..37160757b 100644 --- a/lib-protocol/source/connection.hpp +++ b/lib-protocol/source/connection.hpp @@ -81,12 +81,10 @@ class Association : co_return writ; } - task Open(const Socket &endpoint) { orc_ahead orc_block({ try { + task Open(const Socket &endpoint) { orc_ahead orc_block({ co_await association_.async_connect(endpoint, Adapt()); association_.non_blocking(true); - } catch (const asio::system_error &error) { - orc_adapt(error); - } }, "connecting to " << endpoint); } + }, "connecting to " << endpoint); } void Shut() noexcept override { association_.close(); diff --git a/lib-shared/source/error.hpp b/lib-shared/source/error.hpp index be0368b22..e541b05da 100644 --- a/lib-shared/source/error.hpp +++ b/lib-shared/source/error.hpp @@ -70,13 +70,6 @@ class Error final : throw orc_log(orc::Error(), text); \ } while (false) -#define orc_adapt(error) do { \ - const auto code(error.code()); \ - if (code.category() == orchid_category()) \ - std::rethrow_exception(Category::Convert(code.value())); \ - orc_throw(code.message() << " (" << code << ")"); \ -} while (false) - #define orc_assert_(code, text) do { \ if ((code)) break; \ orc_throw(text); \