A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
exception_guard.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
78#ifndef A11_EXCEPTION_GUARD_H_
79#define A11_EXCEPTION_GUARD_H_
80
81#include <exception>
82#include <functional>
83#include <string_view>
84#include <utility>
85
86#include <absl/functional/any_invocable.h>
87#include <absl/status/status.h>
88
89namespace a11::exception_guard {
90
91namespace internal {
92
93// / The status `what` raising `error` becomes. Shared so the wording is one
94// thing.
95absl::Status Raised(const std::exception& error, std::string_view what);
97absl::Status RaisedUnknown(std::string_view what);
98
99} // namespace internal
100
113template <typename Callable>
114absl::Status Attempt([[maybe_unused]] Callable&& callable,
115 [[maybe_unused]] std::string_view what) {
116#if defined(__cpp_exceptions) || defined(__EXCEPTIONS)
117 try {
118 std::forward<Callable>(callable)();
119 } catch (const std::exception& error) {
120 return internal::Raised(error, what);
121 } catch (...) {
122 return internal::RaisedUnknown(what);
123 }
124 return absl::OkStatus();
125#else
126 std::forward<Callable>(callable)();
127 return absl::OkStatus();
128#endif
129}
130
145template <typename Result, typename... Args>
146std::function<Result(Args...)> Wrap(std::function<Result(Args...)> callable,
147 std::string_view what);
148
150template <typename Result, typename... Args>
151absl::AnyInvocable<Result(Args...)> WrapOnce(
152 absl::AnyInvocable<Result(Args...)> callable, std::string_view what);
153
155template <typename Result, typename... Args>
156absl::AnyInvocable<Result(Args...) &&> WrapConsuming(
157 absl::AnyInvocable<Result(Args...) &&> callable, std::string_view what);
158
159} // namespace a11::exception_guard
160
161#endif // A11_EXCEPTION_GUARD_H_
absl::Status RaisedUnknown(std::string_view what)
The same, for something thrown that is not a std::exception.
Definition exception_guard.cc:36
absl::Status Raised(const std::exception &error, std::string_view what)
Definition exception_guard.cc:32
Definition boundary.cc:37
absl::Status Attempt(Callable &&callable, std::string_view what)
Runs callable, reporting anything it throws as a Status.
Definition exception_guard.h:114
std::function< Result(Args...)> Wrap(std::function< Result(Args...)> callable, std::string_view what)
Wraps callable so that anything it throws becomes a failure.
absl::AnyInvocable< Result(Args...)> WrapOnce(absl::AnyInvocable< Result(Args...)> callable, std::string_view what)
Wrap for a move-only callable.
absl::AnyInvocable< Result(Args...) && > WrapConsuming(absl::AnyInvocable< Result(Args...) && > callable, std::string_view what)
Wrap for a callable that is consumed by its one invocation.
absl::Status error
Definition runtime.cc:643