Skip to content

Commit

Permalink
Correct implementation of std::error_code wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
saurik committed Jul 21, 2024
1 parent e71727d commit 935542b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 25 deletions.
8 changes: 4 additions & 4 deletions lib-protocol/source/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class Adapter {
U<Stream> stream_;

template <typename Handler_>
void Convert(Handler_ &&handler, const std::exception_ptr &error) {
boost::asio::post(get_executor(), [handler = std::forward<Handler_>(handler), error = Category::Convert(error)]() mutable {
void Convert(Handler_ &&handler) {
boost::asio::post(get_executor(), [handler = std::forward<Handler_>(handler), error = Category::Convert(std::current_exception())]() mutable {
std::move(handler)(error, 0);
});
}
Expand Down Expand Up @@ -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__);
}
Expand All @@ -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__);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,15 @@ namespace {

std::string Category::message(int index) const {
try {
std::rethrow_exception(Convert(index));
const std::unique_lock<std::mutex> 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<std::mutex> 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<std::mutex> lock(mutex_);
// XXX: clang-tidy might need fixing, as this just bans ?:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -53,4 +51,8 @@ constexpr const boost::system::error_category &orchid_category() noexcept {
return orc::Categories<void>::category_;
}

#define orc_adapt(error) do { \
orc_throw(error.what()); \
} while (false)

#endif//ORCHID_CATEGORY_HPP
6 changes: 2 additions & 4 deletions lib-protocol/source/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ class Association :
co_return writ;
}

task<void> Open(const Socket &endpoint) { orc_ahead orc_block({ try {
task<void> 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();
Expand Down
7 changes: 0 additions & 7 deletions lib-shared/source/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand Down

0 comments on commit 935542b

Please sign in to comment.