Seastar
High performance C++ framework for concurrent servers
exception.hh
1 /*
2  * This file is open source software, licensed to you under the terms
3  * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4  * distributed with this work for additional information regarding copyright
5  * ownership. You may not use this file except in compliance with the License.
6  *
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  * KIND, either express or implied. See the License for the
15  * specific language governing permissions and limitations
16  * under the License.
17  */
18 /*
19  * Copyright (C) 2021-present ScyllaDB
20  */
21 
22 #pragma once
23 
24 #include <seastar/core/future.hh>
25 #include <coroutine>
26 #include <exception>
27 
28 namespace seastar {
29 
30 namespace internal {
31 
32 struct exception_awaiter {
33  std::exception_ptr eptr;
34 
35  explicit exception_awaiter(std::exception_ptr&& eptr) noexcept : eptr(std::move(eptr)) {}
36 
37  exception_awaiter(const exception_awaiter&) = delete;
38  exception_awaiter(exception_awaiter&&) = delete;
39 
40  bool await_ready() const noexcept {
41  return false;
42  }
43 
44  template<typename U>
45  void await_suspend(std::coroutine_handle<U> hndl) noexcept {
46  hndl.promise().set_exception(std::move(eptr));
47  hndl.destroy();
48  }
49 
50  void await_resume() noexcept {}
51 };
52 
53 } // internal
54 
55 namespace coroutine {
56 
71 struct exception {
72  std::exception_ptr eptr;
73  explicit exception(std::exception_ptr eptr) noexcept : eptr(std::move(eptr)) {}
74 };
75 
91 [[deprecated("Use co_await coroutine::return_exception or co_return coroutine::exception instead")]]
92 [[nodiscard]]
93 inline exception make_exception(std::exception_ptr ex) noexcept {
94  return exception(std::move(ex));
95 }
96 
97 template<typename T>
98 [[deprecated("Use co_await coroutine::return_exception or co_return coroutine::exception instead")]]
99 [[nodiscard]]
100 exception make_exception(T&& t) noexcept {
101  log_exception_trace();
102  return exception(std::make_exception_ptr(std::forward<T>(t)));
103 }
104 
117 [[nodiscard]]
118 inline exception return_exception_ptr(std::exception_ptr ex) noexcept {
119  return exception(std::move(ex));
120 }
121 
122 [[deprecated("Use co_await coroutine::return_exception_ptr instead")]]
123 [[nodiscard]]
124 inline exception return_exception(std::exception_ptr ex) noexcept {
125  return exception(std::move(ex));
126 }
127 
128 template<typename T>
129 [[nodiscard]]
130 exception return_exception(T&& t) noexcept {
131  log_exception_trace();
132  return exception(std::make_exception_ptr(std::forward<T>(t)));
133 }
134 
135 } // coroutine
136 
137 inline auto operator co_await(coroutine::exception ex) noexcept {
138  return internal::exception_awaiter(std::move(ex.eptr));
139 }
140 
141 } // seastar
promise - allows a future value to be made available at a later time.
Definition: future.hh:926
void set_exception(std::exception_ptr &&ex) noexcept
Marks the promise as failed.
Definition: future.hh:990
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: exception.hh:71