23 explicit ffi_guard(Func f) : func(std::move(f)) {}
25 template <
typename... Args>
26 auto operator()(Args &&...args) ->
decltype(func(std::forward<Args>(args)...)) {
29 ::ErrorContextCallback *cb;
31 ::MemoryContext mcxt = ::CurrentMemoryContext;
33 pbuf = ::PG_exception_stack;
34 cb = ::error_context_stack;
35 ::PG_exception_stack = &buf;
38 std::shared_ptr<void> defer(
nullptr, [&](...) {
39 ::error_context_stack = cb;
40 ::PG_exception_stack = pbuf;
43 state = sigsetjmp(buf, 1);
46 return func(std::forward<Args>(args)...);
47 }
else if (state == 1) {
50 __builtin_unreachable();
69 elog(WARNING,
"%s", warning_message);
84 const char *warning_message;
85 int uncaught = std::uncaught_exceptions();
87 scope_fail(Func f,
const char *warning_message)
88 : func(std::move(f)), warning_message(warning_message) {}
91 if (std::uncaught_exceptions() > uncaught) {
114 const char *warning_message;
116 scope_exit(Func f,
const char *warning_message)
117 : func(std::move(f)), warning_message(warning_message) {}
147 template <
typename... Args>
148 auto operator()(Args &&...args) ->
decltype(func(std::forward<Args>(args)...)) {
150 return func(std::forward<Args>(args)...);
153 }
catch (
const std::exception &e) {
154 report(ERROR,
"exception: %s", e.what());
156 report(ERROR,
"some exception occurred");
158 __builtin_unreachable();
Definition: exception.hpp:7
void rethrow()
Re-throws the captured error as a Postgres error, with every field (SQLSTATE, detail,...
Definition: exception_impl.hpp:22
void ffi_guard_noexcept(Func f, const char *warning_message) noexcept
Runs a callable under cppgres::ffi_guard, degrading any failure to a warning.
Definition: guard.hpp:65
Wraps a C++ function to catch exceptions and report them as Postgres errors.
Definition: guard.hpp:142
Scope guard that always runs the callable on scope exit.
Definition: guard.hpp:112
Scope guard that runs the callable only when the scope is left via an exception.
Definition: guard.hpp:82