Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
system-error.hh
Go to the documentation of this file.
1#pragma once
10
11#include "nix/util/error.hh"
12
13#include <cerrno>
14#include <concepts>
15#include <cstring>
16#include <string>
17#include <string_view>
18#include <system_error>
19#include <type_traits>
20#include <utility>
21
22#ifdef _WIN32
23# include <errhandlingapi.h>
24#endif
25
26namespace nix {
27
33class SystemError : public CloneableError<SystemError, Error>
34{
35 std::error_code errorCode;
36 std::string errorDetails;
37
38 void anchor() override;
39
40protected:
41
49 {};
50
58 {};
59
65 SystemError(DisambigHintFmt, std::error_code errorCode, std::string_view errorDetails, const HintFmt & hf)
66 : CloneableError(HintFmt{"%s: %s", Uncolored(hf.str()), errorDetails})
67 , errorCode(errorCode)
68 , errorDetails(errorDetails)
69 {
70 }
71
76 template<typename... Args>
77 SystemError(DisambigVarArgs, std::error_code errorCode, std::string_view errorDetails, Args &&... args)
78 : SystemError(DisambigHintFmt{}, errorCode, errorDetails, HintFmt{std::forward<Args>(args)...})
79 {
80 }
81
82public:
87 SystemError(std::error_code errorCode, const HintFmt & hf)
88 : SystemError(DisambigHintFmt{}, errorCode, errorCode.message(), hf)
89 {
90 }
91
96 template<typename... Args>
97 SystemError(std::error_code errorCode, Args &&... args)
98 : SystemError(DisambigVarArgs{}, errorCode, errorCode.message(), std::forward<Args>(args)...)
99 {
100 }
101
102 const std::error_code ec() const &
103 {
104 return errorCode;
105 }
106
107 bool is(std::errc e) const
108 {
109 return errorCode == e;
110 }
111
112 template<std::same_as<std::errc>... Rest>
113 bool is(std::errc e, Rest... rest) const
114 {
115 return is(e) || (is(rest) || ...);
116 }
117};
118
155template<typename Fields, auto field>
156 requires std::is_member_object_pointer_v<decltype(field)>
157class NativeCodeSystemError : public Fields, public SystemError
158{
159public:
160 /* `field` is a plain `auto`, constrained to be a pointer to a member,
161 rather than `auto Fields::*`: GCC cannot deduce that placeholder,
162 failing to use the preceding `Fields` for it. Applying `field` to a
163 `Fields` here still checks that it is a member of `Fields`. */
164 using NativeCode = std::remove_cvref_t<decltype(std::declval<Fields &>().*field)>;
165
170 template<typename... Args>
171 NativeCodeSystemError(NativeCode code, Args &&... args)
172 : Fields{code}
173 , SystemError(
174 DisambigVarArgs{}, Fields::toErrorCode(code), Fields::renderError(code), std::forward<Args>(args)...)
175 {
176 }
177
185 NativeCodeSystemError(NativeCode code, const HintFmt & hf)
186 : Fields{code}
187 , SystemError(DisambigHintFmt{}, Fields::toErrorCode(code), Fields::renderError(code), hf)
188 {
189 }
190
197 template<typename... Args>
199 : NativeCodeSystemError(Fields::ambientError(), std::forward<Args>(args)...)
200 {
201 }
202
208 NativeCodeSystemError(auto && mkHintFmt)
209 requires std::invocable<decltype(mkHintFmt)> && std::same_as<std::invoke_result_t<decltype(mkHintFmt)>, HintFmt>
210 : NativeCodeSystemError(captureAmbientError(std::forward<decltype(mkHintFmt)>(mkHintFmt)))
211 {
212 }
213
214private:
221 static std::pair<NativeCode, HintFmt> captureAmbientError(auto && mkHintFmt)
222 {
223 NativeCode e = Fields::ambientError();
224 return {e, mkHintFmt()};
225 }
226
227 NativeCodeSystemError(std::pair<NativeCode, HintFmt> && p)
228 : NativeCodeSystemError(p.first, std::move(p.second))
229 {
230 }
231};
232
238{
239 int errNo;
240
244 static int ambientError()
245 {
246 return errno;
247 }
248
252 static std::error_code toErrorCode(int code)
253 {
254 return std::make_error_code(static_cast<std::errc>(code));
255 }
256
260 static std::string renderError(int code)
261 {
262 return strerror(code);
263 }
264};
265
282class SysError final : public CloneableError<SysError, NativeCodeSystemError<SysErrorFields, &SysErrorFields::errNo>>
283{
284 void anchor() override;
285
286public:
287 using CloneableError::CloneableError;
288};
289
290#ifdef _WIN32
291
292namespace windows {
293
298struct WinErrorFields
299{
300 DWORD lastError;
301
305 static DWORD ambientError()
306 {
307 return GetLastError();
308 }
309
313 static std::error_code toErrorCode(DWORD code)
314 {
315 return std::error_code(code, std::system_category());
316 }
317
321 static std::string renderError(DWORD code);
322};
323
330class WinError : public CloneableError<WinError, NativeCodeSystemError<WinErrorFields, &WinErrorFields::lastError>>
331{
332 void anchor() override;
333
334public:
335 using CloneableError::CloneableError;
336};
337
338} // namespace windows
339
340#endif
341
348#ifdef _WIN32
349 windows::WinError
350#else
352#endif
353 ;
354
359using NativeError = NativeSysError::NativeCode;
360
361} // namespace nix
Definition args.hh:29
std::string message() const
Definition error.hh:192
Definition fmt.hh:169
Definition system-error.hh:158
NativeCodeSystemError(Args &&... args)
Definition system-error.hh:198
NativeCodeSystemError(NativeCode code, const HintFmt &hf)
Definition system-error.hh:185
NativeCodeSystemError(NativeCode code, Args &&... args)
Definition system-error.hh:171
NativeCodeSystemError(auto &&mkHintFmt)
Definition system-error.hh:208
Definition system-error.hh:283
SystemError(DisambigHintFmt, std::error_code errorCode, std::string_view errorDetails, const HintFmt &hf)
Definition system-error.hh:65
SystemError(std::error_code errorCode, Args &&... args)
Definition system-error.hh:97
SystemError(DisambigVarArgs, std::error_code errorCode, std::string_view errorDetails, Args &&... args)
Definition system-error.hh:77
SystemError(std::error_code errorCode, const HintFmt &hf)
Definition system-error.hh:87
This file defines two main structs/classes used in nix error handling.
Definition system-error.hh:238
static int ambientError()
Definition system-error.hh:244
static std::string renderError(int code)
Definition system-error.hh:260
static std::error_code toErrorCode(int code)
Definition system-error.hh:252
Definition system-error.hh:49
Definition system-error.hh:58
Definition fmt.hh:149
NativeSysError::NativeCode NativeError
Definition system-error.hh:359
SysError NativeSysError
Definition system-error.hh:347