Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ErrorHandling.hpp
1#pragma once
2
3#include "util/Assert.hpp"
4#include "util/async/Concepts.hpp"
5#include "util/async/Error.hpp"
6
7#include <fmt/format.h>
8#include <fmt/std.h>
9
10#include <exception>
11#include <expected>
12#include <thread>
13
14namespace util::async::impl {
15
17 [[nodiscard]] static auto
18 wrap(auto&& fn) noexcept
19 {
20 return [fn = std::forward<decltype(fn)>(fn)]<typename... Args>(
21 SomeOutcome auto& outcome, Args&&... args
22 ) mutable {
23 try {
24 std::invoke(std::forward<decltype(fn)>(fn), outcome, std::forward<Args>(args)...);
25 } catch (std::exception const& e) {
26 outcome.setValue(
27 std::unexpected(
28 ExecutionError{fmt::format("{}", std::this_thread::get_id()), e.what()}
29 )
30 );
31 } catch (...) {
32 outcome.setValue(
33 std::unexpected(
34 ExecutionError{fmt::format("{}", std::this_thread::get_id()), "unknown"}
35 )
36 );
37 }
38 };
39 }
40
41 [[nodiscard]] static auto
42 catchAndAssert(
43 auto&& fn
44 ) noexcept // note this is a lie when used with MockAssert (use MockAssertNoThrow)
45 {
46 return [fn = std::forward<decltype(fn)>(fn)] mutable {
47 try {
48 std::invoke(std::forward<decltype(fn)>(fn));
49 } catch (std::exception const& e) {
50 ASSERT(false, "Exception caught: {}", e.what());
51 } catch (...) {
52 ASSERT(false, "Unknown exception caught");
53 }
54 };
55 }
56};
57
59 [[nodiscard]] static constexpr auto
60 wrap(auto&& fn)
61 {
62 return std::forward<decltype(fn)>(fn);
63 }
64
65 [[nodiscard]] static constexpr auto
66 catchAndAssert(auto&& fn)
67 {
68 return std::forward<decltype(fn)>(fn);
69 }
70};
71
72} // namespace util::async::impl
Specifies the interface for an outcome (promise).
Definition Concepts.hpp:105
Error channel type for async operation of any ExecutionContext.
Definition Error.hpp:19
Definition ErrorHandling.hpp:16
Definition ErrorHandling.hpp:58