]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
crimson/errorator: helpers for tl::expected
authorRadoslaw Zarzynski <rzarzyns@redhat.com>
Mon, 19 Jan 2026 22:27:19 +0000 (22:27 +0000)
committerRadoslaw Zarzynski <rzarzyns@redhat.com>
Tue, 27 Jan 2026 19:42:59 +0000 (19:42 +0000)
`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<my_value_t> 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<my_ertr_t>::from_error<my_value_t>(maybe_decoded.error());
  }
  // ...
}
```

Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
(cherry picked from commit a7ffc18eb40b9145fca5f064c4b3d097bffeb8d0)

src/crimson/common/errorator-utils.h

index 2af1c4ba4c0ce4cc1902ed9c0fd2189d931d40fd..b1dd6fc3af53b80352796c0df21597a5054e6c53 100644 (file)
@@ -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 <class>
+struct ErrorHelper;
+template <class... AllowedErrors>
+struct ErrorHelper<crimson::errorator<AllowedErrors...>>
+{
+  // wrapped error to raw errror
+  template <class ErrorT, ErrorT ErrorV>
+  static auto to_error(const unthrowable_wrapper<ErrorT, ErrorV>&)
+  {
+    return std::decay_t<ErrorT>(ErrorV);
+  }
+
+  // raw_error to failed future carrying this error OR assert
+  template <class FutureValueT,
+            class UnexpectedErrorT>
+  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<AllowedErrors...>\
+      ::template make_exception_future2<FutureValueT>(std::move(ep));
+  }
+};
+
 } // namespace crimson