Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
28namespace seastar {
29
30namespace internal {
31
32struct 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
55namespace coroutine {
56
71struct 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_ptr or co_return coroutine::exception instead")]]
92[[nodiscard]]
93inline exception make_exception(std::exception_ptr ex) noexcept {
94 return exception(std::move(ex));
95}
96
97template<typename T>
98[[deprecated("Use co_await coroutine::return_exception_ptr or co_return coroutine::exception instead")]]
99[[nodiscard]]
100exception make_exception(T&& t) noexcept {
101 log_exception_trace();
102 return exception(std::make_exception_ptr(std::forward<T>(t)));
103}
104
125[[nodiscard]]
126inline exception return_exception_ptr(std::exception_ptr ex) noexcept {
127 return exception(std::move(ex));
128}
129
142[[deprecated("Use co_await coroutine::return_exception_ptr instead")]]
143[[nodiscard]]
144inline exception return_exception(std::exception_ptr ex) noexcept {
145 return exception(std::move(ex));
146}
147
148template<typename T>
149[[nodiscard]]
150exception return_exception(T&& t) noexcept {
151 log_exception_trace();
152 return exception(std::make_exception_ptr(std::forward<T>(t)));
153}
154
155} // coroutine
156
157inline auto operator co_await(coroutine::exception ex) noexcept {
158 return internal::exception_awaiter(std::move(ex.eptr));
159}
160
161} // seastar
promise - allows a future value to be made available at a later time.
Definition: future.hh:934
void set_exception(std::exception_ptr &&ex) noexcept
Marks the promise as failed.
Definition: future.hh:998
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: exception.hh:71