1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
// This source code is released into the public domain.
export module nihil.error;
// error: a type representing an error.
//
// An error consists of an immediate cause, which may be a string or
// std:error_code, and an optional proximate cause, which is another error
// object. Any number of error objects may be stacked.
//
// For example, a failure to open a file might be a stack of two errors:
//
// - string, "failed to open /etc/somefile",
// - std::error_code, "No such file or directory".
//
// Calling .str() will format the entire stack starting at that error,
// for example: "failed to open /etc/somefile: No such file or directory".
//
// Errors may be moved and (relatively) cheaply copied, since the cause
// chain is refcounted.
//
// error derives from std::exception, so it may be thrown and caught and
// provides a useful what(). When throwing errors, creating a derived
// error will make it easier to distinguish errors when catching them.
import nihil.std;
import nihil.match;
export import :sys_error;
namespace nihil {
// Things which can be errors.
using error_t = std::variant<std::monostate, std::string, std::error_code, std::error_condition>;
// A proxy class used when constructing errors. This has implicit constructors from various types,
// which means we don't have to handle every possible combination of types in error itself.
export struct error_proxy
{
// Construct from...
// ... a string_view
error_proxy(std::string_view const what) // NOLINT
: m_error(std::string(what))
{
}
// ... an std::string; so we can move the string into place if it's an rvalue.
error_proxy(auto &&what) // NOLINT
requires(std::same_as<std::remove_cvref_t<decltype(what)>, std::string>)
: m_error(std::forward<decltype(what)>(what))
{
}
// ... a C string
error_proxy(char const *what) // NOLINT
: m_error(std::string(what))
{
}
// ... an std::error_code
error_proxy(std::error_code const what) // NOLINT
: m_error(what)
{
}
// ... an std::error_condition
error_proxy(std::error_condition const what) // NOLINT
: m_error(what)
{
}
// ... an error code enum
template <typename T>
requires(std::is_error_code_enum<T>::value)
error_proxy(T what) // NOLINT
: m_error(make_error_code(what))
{
}
// ... an error condition enum
template <typename T>
requires(std::is_error_condition_enum<T>::value)
error_proxy(T what) // NOLINT
: m_error(make_error_condition(what))
{
}
// Not copyable.
error_proxy(error_proxy const &) = delete;
auto operator=(error_proxy const &) -> error_proxy & = delete;
// Not movable.
error_proxy(error_proxy &&) = delete;
auto operator=(error_proxy &&) -> error_proxy & = delete;
~error_proxy() = default;
// Let error extract the error_t.
[[nodiscard]] auto error() && -> error_t &&
{
return std::move(m_error);
}
private:
// The error.
error_t m_error;
};
// The error class.
export struct error : std::exception
{
// Create an empty error, representing success.
error() = default;
// Destroy an error.
~error() override = default;
// Create an error from an error proxy.
explicit error(error_proxy &&proxy)
: m_error(std::move(std::move(proxy).error()))
{
}
// Create an error from an error proxy and an error cause.
error(error_proxy &&proxy, error cause)
: m_error(std::move(std::move(proxy).error()))
, m_cause(std::make_shared<error>(std::move(cause)))
{
}
// Create an error from an error proxy and an error_proxy cause.
// For example, error("cannot open file", std::errc::permission_denied).
error(error_proxy &&proxy, error_proxy &&cause)
: m_error(std::move(std::move(proxy).error()))
, m_cause(std::make_shared<error>(std::move(cause)))
{
}
// Copyable.
error(error const &) = default;
auto operator=(error const &) -> error & = default;
// Movable.
error(error &&) noexcept = default;
auto operator=(error &&) noexcept -> error & = default;
// Return the cause of this error.
[[nodiscard]] auto cause(this error const &self) -> std::shared_ptr<error> const &
{
return self.m_cause;
}
// Return the root cause of this error, which may be this object.
// For errors caused by an OS error, this will typically be the
// error_code error.
[[nodiscard]] auto root_cause(this error const &self) -> error const &
{
auto const *cause = &self;
while (cause->m_cause)
cause = cause->m_cause.get();
return *cause;
}
// Format this error without its cause as a string.
[[nodiscard]] auto this_str(this error const &self) -> std::string
{
return self.m_error | match {
[] (std::monostate) -> std::string {
return "No error";
},
[] (std::error_code const &m) {
return m.message();
},
[] (std::error_condition const &m) {
return m.message();
},
[] (std::string const &m) {
return m;
}
};
}
// Format this error and its cause as a string.
[[nodiscard]] auto full_str(this error const &self) -> std::string
{
auto str = self.this_str();
auto cause = self.cause();
while (cause) {
str += ": " + cause->this_str();
cause = cause->cause();
}
return str;
}
// Return this error's error_code, if any.
[[nodiscard]] auto code(this error const &self) -> std::optional<std::error_code>
{
auto const *code = std::get_if<std::error_code>(&self.m_error);
if (code != nullptr)
return {*code};
return {};
}
// Return this error's error_condition, if any.
[[nodiscard]] auto condition(this error const &self) -> std::optional<std::error_condition>
{
auto const *condition = std::get_if<std::error_condition>(&self.m_error);
if (condition != nullptr)
return {*condition};
return {};
}
// Format this error and its cause as a C string and return it. This is for
// compatibility with std::exception. The lifetime of the returned string
// is the same as the error object.
[[nodiscard]] auto what() const noexcept -> char const * final
{
if (!m_what)
m_what = this->full_str();
return m_what->c_str();
}
// Allow error to be implicitly converted to std::expected and std::unexpected, to make
// using it with std::expected easier.
template <typename T>
operator std::expected<T, error> () && // NOLINT
{
return std::unexpected{std::move(*this)};
}
template<typename T>
operator std::expected<T, error> () const // NOLINT
{
return std::unexpected{*this};
}
operator std::unexpected<error> () && // NOLINT
{
return std::unexpected{std::move(*this)};
}
operator std::unexpected<error> () const // NOLINT
{
return std::unexpected{*this};
}
private:
// This error.
error_t m_error = make_error_code(std::errc());
// The cause of this error, if any.
std::shared_ptr<error> m_cause;
// For std::exception::what(), we need to keep the string valid
// until we're destroyed.
mutable std::optional<std::string> m_what;
// Equality comparison.
[[nodiscard]] friend auto operator==(error const &lhs, error const &rhs) -> bool
{
return lhs.m_error == rhs.m_error;
}
[[nodiscard]] friend auto operator<=>(error const &lhs, error const &rhs) -> std::strong_ordering
{
return lhs.m_error <=> rhs.m_error;
}
// Compare an error with an std::error_code.
[[nodiscard]] friend auto operator==(error const &lhs, std::error_code const &rhs) -> bool
{
return lhs.code() == rhs;
}
// Compare an error to an std::error_condition.
[[nodiscard]] friend auto operator==(error const &lhs, std::error_condition const &rhs) -> bool
{
return lhs.condition() == rhs;
}
// Compare an error to an std::error_code enum.
[[nodiscard]] friend auto operator==(error const &lhs, auto rhs) -> bool
requires(std::is_error_code_enum<decltype(rhs)>::value)
{
return lhs.code() == rhs;
}
// Compare an error to an std::error_condition enum.
[[nodiscard]] friend auto operator==(error const &lhs, auto rhs) -> bool
requires(std::is_error_condition_enum<decltype(rhs)>::value)
{
return lhs.condition() == rhs;
}
// Print an error to a stream.
friend auto operator<<(std::ostream &strm, error const &e) -> std::ostream &
{
return strm << e.full_str();
}
};
} // namespace nihil
// Make error formattable.
export template <>
struct std::formatter<nihil::error, char>
{
template <typename ParseContext>
constexpr auto parse(ParseContext &ctx) -> ParseContext::iterator
{
return ctx.begin();
}
template <typename FormatContext>
auto format(nihil::error const &e, FormatContext &ctx) const -> FormatContext::iterator
{
return std::ranges::copy(e.full_str(), ctx.out()).out;
}
};
|