Skip to content

Commit 9f17b63

Browse files
committed
rgw: Add run_coro utility
A convenience function for turning coroutines that return values and use exceptions, `error_code`, or similar into `int`-returning functions that take references to out parameters. Signed-off-by: Adam C. Emerson <[email protected]>
1 parent e81d4ea commit 9f17b63

File tree

4 files changed

+428
-18
lines changed

4 files changed

+428
-18
lines changed

src/common/async/concepts.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include <type_traits>
1919

20+
#include <boost/asio/execution/executor.hpp>
21+
2022
#include <boost/asio/disposition.hpp>
2123
#include <boost/asio/execution_context.hpp>
2224

@@ -32,7 +34,6 @@ template<typename ExecutionContext>
3234
concept execution_context =
3335
std::is_convertible_v<ExecutionContext&,
3436
boost::asio::execution_context&>;
35-
3637
/// A concept for Asio 'disposition's, a generalization of error
3738
/// codes/exception pointers, etc.
3839
template<typename T>

src/common/async/librados_completion.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ struct librados_handler {
9797
}
9898

9999
void operator ()(std::exception_ptr e) {
100-
(*this)(ceph::from_exception(e));
100+
std::string what;
101+
(*this)(ceph::from_exception(e, &what));
101102
}
102103
};
103104
} // namespace detail

src/common/error_code.h

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,57 @@ inline boost::system::error_condition make_error_condition(errc e) noexcept {
9494
#pragma GCC diagnostic pop
9595
#pragma clang diagnostic pop
9696

97-
[[nodiscard]] inline int from_exception(std::exception_ptr eptr) {
97+
[[nodiscard]]
98+
inline int from_exception(std::exception_ptr eptr, std::string* what = nullptr)
99+
{
98100
if (!eptr) [[likely]] {
99101
return 0;
100102
}
101103
try {
102104
std::rethrow_exception(eptr);
103105
} catch (const boost::system::system_error& e) {
106+
if (what)
107+
*what = e.what();
104108
return from_error_code(e.code());
105109
} catch (const std::system_error& e) {
110+
if (what)
111+
*what = e.what();
106112
return from_error_code(e.code());
107-
} catch (const std::invalid_argument&) {
113+
} catch (const std::invalid_argument& e) {
114+
if (what)
115+
*what = e.what();
108116
return -EINVAL;
109-
} catch (const std::domain_error&) {
117+
} catch (const std::domain_error& e) {
118+
if (what)
119+
*what = e.what();
110120
return -EDOM;
111-
} catch (const std::length_error&) {
121+
} catch (const std::length_error& e) {
122+
if (what)
123+
*what = e.what();
112124
return -ERANGE;
113-
} catch (const std::out_of_range&) {
125+
} catch (const std::out_of_range& e) {
126+
if (what)
127+
*what = e.what();
114128
return -ERANGE;
115-
} catch (const std::range_error&) {
129+
} catch (const std::range_error& e) {
130+
if (what)
131+
*what = e.what();
116132
return -ERANGE;
117-
} catch (const std::overflow_error&) {
133+
} catch (const std::overflow_error& e) {
134+
if (what)
135+
*what = e.what();
118136
return -EOVERFLOW;
119-
} catch (const std::underflow_error&) {
137+
} catch (const std::underflow_error& e) {
138+
if (what)
139+
*what = e.what();
120140
return -EOVERFLOW;
121-
} catch (const std::bad_alloc&) {
141+
} catch (const std::bad_alloc& e) {
142+
if (what)
143+
*what = e.what();
122144
return -ENOMEM;
123145
} catch (const std::regex_error& e) {
146+
if (what)
147+
*what = e.what();
124148
using namespace std::regex_constants;
125149
switch (e.code()) {
126150
case error_space:
@@ -134,28 +158,53 @@ inline boost::system::error_condition make_error_condition(errc e) noexcept {
134158
#ifdef __has_include
135159
# if __has_include(<format>)
136160
} catch (const std::format_error& e) {
161+
if (what)
162+
*what = e.what();
137163
return -EINVAL;
138164
# endif
139165
#endif
140166
} catch (const fmt::format_error& e) {
167+
if (what)
168+
*what = e.what();
141169
return -EINVAL;
142-
} catch (const std::bad_typeid&) {
170+
} catch (const std::bad_typeid& e) {
171+
if (what)
172+
*what = e.what();
143173
return -EFAULT;
144-
} catch (const std::bad_cast&) {
174+
} catch (const std::bad_cast& e) {
175+
if (what)
176+
*what = e.what();
145177
return -EFAULT;
146-
} catch (const std::bad_optional_access&) {
178+
} catch (const std::bad_optional_access& e) {
179+
if (what)
180+
*what = e.what();
147181
return -EFAULT;
148-
} catch (const std::bad_weak_ptr&) {
182+
} catch (const std::bad_weak_ptr& e) {
183+
if (what)
184+
*what = e.what();
149185
return -EFAULT;
150-
} catch (const std::bad_function_call&) {
186+
} catch (const std::bad_function_call& e) {
187+
if (what)
188+
*what = e.what();
151189
return -EFAULT;
152-
} catch (const std::bad_exception&) {
190+
} catch (const std::bad_exception& e) {
191+
if (what)
192+
*what = e.what();
153193
return -EFAULT;
154-
} catch (const std::bad_variant_access&) {
194+
} catch (const std::bad_variant_access& e) {
195+
if (what)
196+
*what = e.what();
155197
return -EFAULT;
198+
} catch (const std::exception& e) {
199+
if (what)
200+
*what = e.what();
201+
return -EIO;
156202
} catch (...) {
203+
if (what)
204+
*what = "Unknown exception";
157205
return -EIO;
158206
}
207+
return -EIO;
159208
}
160209
}
161210

0 commit comments

Comments
 (0)