From: Radoslaw Zarzynski Date: Mon, 19 Jan 2026 22:27:19 +0000 (+0000) Subject: crimson/errorator: helpers for tl::expected X-Git-Tag: v21.0.0~3^2~45 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=33b7b7bca044aada1f79615f9ac1e92aa6fd4047;p=ceph.git crimson/errorator: helpers for tl::expected `ErrorHelper` is supposed to facilitate transformation of `std::error_code` (and similar) into an immediately failed errorated `future` with extra error checking. The main idea is to let something like: ```cpp my_ertr_t::future foo() { // ... // `maybe_decoded` here is an instance of `tl::expected` carrying `std::error_code` // in its error part if (!maybe_decoded) { // `std::error_code` can convey errors `my_ertr_t` does not allow – in that case, // it would be helpful to get at least a run-time assert. return ErrorHelper::from_error(maybe_decoded.error()); } // ... } ``` Signed-off-by: Radoslaw Zarzynski (cherry picked from commit a7ffc18eb40b9145fca5f064c4b3d097bffeb8d0) --- diff --git a/src/crimson/common/errorator-utils.h b/src/crimson/common/errorator-utils.h index 2af1c4ba4c0c..6d69c573b7d7 100644 --- a/src/crimson/common/errorator-utils.h +++ b/src/crimson/common/errorator-utils.h @@ -87,4 +87,38 @@ parallel_for_each(Iterator first, Iterator last, Func&& func) noexcept { return seastar::make_ready_future<>(); } +// ErrorHelper -- facilitate transformation of `std::error_code` (and +// similar) into an immediately failed errorated `future` with extra +// checking to ensure we the carried error is within AllowedErrors. +template +struct ErrorHelper; +template +struct ErrorHelper> +{ + // wrapped error to raw errror + template + static auto to_error(const unthrowable_wrapper&) + { + return ErrorT{ErrorV}; + } + + // raw_error to failed future carrying this error OR assert + template + static auto from_error(UnexpectedErrorT raw_ec) + { + std::exception_ptr ep; + (... || [&] mutable { + if (const auto& err = AllowedErrors::make(); to_error(err) == raw_ec) { + ep = AllowedErrors::exception_ptr(); + return true; // done + } + return false; // continue + }()); + ceph_assert_always(ep); + return errorator\ + ::template make_exception_future2(std::move(ep)); + } +}; + } // namespace crimson