raft 0.1.0
Loading...
Searching...
No Matches
errors.hpp
1#pragma once
2
3#include <fmt/core.h>
4#include <fmt/std.h>
5
6#include "raft/errors.hpp"
7
8namespace raft::errors::detail
9{
10 template<typename T>
11 constexpr std::string_view getMessage()
12 {
13 if constexpr (std::is_same_v<T, Timeout>)
14 {
15 return "timeout";
16 }
17 if constexpr (std::is_same_v<T, Unimplemented>)
18 {
19 return "unimplemented";
20 }
21 if constexpr (std::is_same_v<T, NotLeader>)
22 {
23 return "not leader";
24 }
25 if constexpr (std::is_same_v<T, AlreadyRunning>)
26 {
27 return "already running";
28 }
29 if constexpr (std::is_same_v<T, NotRunning>)
30 {
31 return "not running";
32 }
33 if constexpr (std::is_same_v<T, FailedToStart>)
34 {
35 return "failed to start";
36 }
37 if constexpr (std::is_same_v<T, Deserialization>)
38 {
39 return "deserialization";
40 }
41 if constexpr (std::is_same_v<T, UnknownLeader>)
42 {
43 return "unknown leader";
44 }
45 if constexpr (std::is_same_v<T, NonexistentNetwork>)
46 {
47 return "nonexistent network";
48 }
49 if constexpr (std::is_same_v<T, NoPersistedState>)
50 {
51 return "no persisted state";
52 }
53 return "unknown error";
54 }
55
56 template<typename T>
57 concept SimpleError = std::is_same_v<T, Timeout> || std::is_same_v<T, Unimplemented>
58 || std::is_same_v<T, NotLeader> || std::is_same_v<T, AlreadyRunning>
59 || std::is_same_v<T, NotRunning> || std::is_same_v<T, FailedToStart>
60 || std::is_same_v<T, Deserialization> || std::is_same_v<T, UnknownLeader>
61 || std::is_same_v<T, NonexistentNetwork> || std::is_same_v<T, NoPersistedState>;
62} // namespace raft::errors::detail
63
64template<>
65struct fmt::formatter<raft::errors::Unknown>
66{
67 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
68
69 template<typename FormatContext>
70 auto format(raft::errors::Unknown const& err, FormatContext& ctx) const
71 {
72 return fmt::format_to(ctx.out(), "unknown error: {}", err.message);
73 }
74};
75
76template<>
77struct fmt::formatter<raft::errors::InvalidArgument>
78{
79 // No format specifiers needed, so the parse function is simple.
80 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
81
82 // The format function defines the output.
83 template<typename FormatContext>
84 auto format(raft::errors::InvalidArgument const& err, FormatContext& ctx) const
85 {
86 return fmt::format_to(ctx.out(), "invalid argument: {}", err.message);
87 }
88};
89
90template<>
91struct fmt::formatter<raft::errors::PersistenceFailed>
92{
93 // No format specifiers needed, so the parse function is simple.
94 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
95
96 // The format function defines the output.
97 template<typename FormatContext>
98 auto format(raft::errors::PersistenceFailed const& err, FormatContext& ctx) const
99 {
100 return fmt::format_to(ctx.out(), "persistence failed: {}", err.message);
101 }
102};
103
104template<raft::errors::detail::SimpleError T>
105struct fmt::formatter<T>
106{
107 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
108
109 template<typename FormatContext>
110 auto format(T const& err, FormatContext& ctx) const
111 {
112 (void)err;
113 return fmt::format_to(ctx.out(), "{}", raft::errors::detail::getMessage<T>());
114 }
115};
Definition errors.hpp:57
An invalid argument error.
Definition errors.hpp:29
std::string message
The error message.
Definition errors.hpp:30
Persistence operation failed.
Definition errors.hpp:75
std::string message
The error message.
Definition errors.hpp:76
An unknown error.
Definition errors.hpp:13
std::string message
The error message.
Definition errors.hpp:14