Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
Loading...
Searching...
No Matches
error.hh
Go to the documentation of this file.
1
#pragma once
17
18
#include "
nix/util/suggestions.hh
"
19
#include "
nix/util/fmt.hh
"
20
#include "
nix/util/fun.hh
"
21
#include "nix/util/config.hh"
22
23
#include <concepts>
24
#include <exception>
25
#include <list>
26
#include <memory>
27
#include <optional>
28
#include <utility>
29
#include <algorithm>
30
#include <concepts>
31
#include <type_traits>
32
33
namespace
nix {
34
35
enum class
Verbosity :
unsigned
{
36
lvlError = 0,
37
lvlWarn,
38
lvlNotice,
39
lvlInfo,
40
lvlTalkative,
41
lvlChatty,
42
lvlDebug,
43
lvlVomit,
44
};
45
46
/* For less churn. */
47
using
enum Verbosity;
48
49
template
<std::
int
egral T>
50
requires
(std::is_unsigned_v<T> && !std::is_same_v<bool, T>)
51
inline
Verbosity verbosityFromIntClamped(T val)
52
{
53
/* Clamp, same as https://git.lix.systems/lix-project/lix/commit/f2fff1faa4c6a308ad30da691e18ceccf6626e0d and
54
what was effectively the behavior before b9f8c4af4057c2ed0ec5d1ff16ac49e0612ad57c because logErrorInfo
55
and friends do a <= nix::verbosity. */
56
return
static_cast<
Verbosity
>
(std::min<T>(val, std::to_underlying(lvlVomit)));
57
}
58
59
/* So that we don't accidentally run into 2s complement conversion issues.
60
Nothing actually needs this conversion. */
61
template
<std::
int
egral T>
62
requires
std::is_signed_v<T>
63
Verbosity verbosityFromIntClamped(T) =
delete
;
64
68
struct
LinesOfCode
69
{
70
std::optional<std::string> prevLineOfCode;
71
std::optional<std::string> errLineOfCode;
72
std::optional<std::string> nextLineOfCode;
73
};
74
75
/* NOTE: position.hh recursively depends on source-path.hh -> source-accessor.hh
76
-> hash.hh -> configuration.hh -> experimental-features.hh -> error.hh -> Pos.
77
There are other such cycles.
78
Thus, Pos has to be an incomplete type in this header. But since ErrorInfo/Trace
79
have to refer to Pos, they have to use pointer indirection via std::shared_ptr
80
to break the recursive header dependency.
81
FIXME: Untangle this mess. Should there be AbstractPos as there used to be before
82
4feb7d9f71? */
83
struct
Pos
;
84
85
void
printCodeLines(std::ostream & out,
const
std::string & prefix,
const
Pos
& errPos,
const
LinesOfCode
& loc);
86
90
enum struct
TracePrint
{
94
Default
,
96
Always
,
97
};
98
99
struct
Trace
100
{
101
std::shared_ptr<const Pos> pos;
102
HintFmt
hint;
103
TracePrint
print =
TracePrint::Default
;
104
};
105
106
inline
std::strong_ordering operator<=>(
const
Trace
& lhs,
const
Trace
& rhs);
107
108
struct
ErrorInfo
109
{
110
Verbosity level;
111
HintFmt
msg;
112
std::shared_ptr<const Pos> pos;
113
std::list<Trace> traces;
118
bool
isFromExpr
=
false
;
119
bool
noIndent =
false
;
120
124
unsigned
int
status
= 1;
125
126
Suggestions
suggestions;
127
};
128
129
std::ostream & showErrorInfo(std::ostream & out,
const
ErrorInfo
& einfo,
bool
showTrace);
130
135
class
BaseError :
public
std::exception
136
{
137
/* VTable anchor to avoid weak linkage of the vtable - it breaks
138
dynamic_cast across shared libraries on Darwin. */
139
virtual
void
anchor();
140
141
protected
:
142
mutable
ErrorInfo
err;
143
147
mutable
std::optional<std::string>
what_
;
151
const
std::string &
calcWhat
()
const
;
152
153
public
:
154
BaseError(
const
BaseError &) =
default
;
155
BaseError & operator=(
const
BaseError &) =
default
;
156
BaseError & operator=(BaseError &&) noexcept = default;
157
158
template<typename...
Args
>
159
BaseError(
unsigned
int
status,
Args
&&... args)
160
: err{.level = lvlError, .msg =
HintFmt
(std::forward<Args>(args)...), .pos = {}, .status = status}
161
{
162
}
163
164
template
<
typename
... Args>
165
explicit
BaseError(
const
std::string & fs, Args &&... args)
166
: err{.level = lvlError, .msg = HintFmt(fs, std::forward<Args>(args)...), .pos = {}}
167
{
168
}
169
170
template
<
typename
...
Args
>
171
BaseError
(
const
Suggestions
& sug,
Args
&&... args)
172
: err{.level = lvlError, .msg =
HintFmt
(std::forward<
Args
>(args)...), .pos = {}, .suggestions = sug}
173
{
174
}
175
176
BaseError(HintFmt hint)
177
: err{.level = lvlError, .msg = hint, .pos = {}}
178
{
179
}
180
181
BaseError(ErrorInfo && e)
182
: err(std::move(e))
183
{
184
}
185
186
BaseError(
const
ErrorInfo & e)
187
: err(e)
188
{
189
}
190
192
std::string
message
()
const
193
{
194
return
err.msg.str();
195
}
196
197
const
char
* what() const noexcept
override
198
{
199
return
calcWhat().c_str();
200
}
201
202
const
std::string & msg()
const
203
{
204
return
calcWhat();
205
}
206
207
const
ErrorInfo & info()
const
208
{
209
calcWhat();
210
return
err;
211
}
212
213
void
withExitStatus(
unsigned
int
status)
214
{
215
err.status = status;
216
}
217
218
void
atPos(std::shared_ptr<const Pos> pos)
219
{
220
err.pos = pos;
221
}
222
223
bool
hasPos()
const
;
224
225
void
pushTrace(Trace trace)
226
{
227
err.traces.push_front(trace);
228
}
229
237
template
<
typename
... Args>
238
void
addTrace
(std::shared_ptr<const Pos> && pos, std::string_view fs,
Args
&&... args)
239
{
240
addTrace
(std::move(pos),
HintFmt
(std::string(fs), std::forward<Args>(args)...));
241
}
242
250
void
addTrace(std::shared_ptr<const Pos> && pos,
HintFmt
hint,
TracePrint
print =
TracePrint::Default
);
251
252
bool
hasTrace()
const
253
{
254
return
!err.traces.empty();
255
}
256
263
ErrorInfo
&
unsafeInfo
()
264
{
265
return
err;
266
}
267
272
void
recalcWhat()
const
;
273
274
[[noreturn]]
virtual
void
throwClone()
const
= 0;
275
};
276
277
template
<
typename
Derived,
typename
Base>
278
class
CloneableError :
public
Base
279
{
280
friend
Derived;
281
CloneableError() =
default
;
282
using
Base::Base;
283
public
:
284
289
[[noreturn]]
void
throwClone
()
const override
290
{
291
throw
Derived(
static_cast<
const
Derived &
>
(*
this
));
292
}
293
};
294
300
[[noreturn]]
void
rethrowExceptionCopy(std::exception_ptr ex);
301
302
#define MakeError(newClass, superClass) \
303
class newClass : public CloneableError<newClass, superClass> \
304
{ \
305
void anchor() override; \
306
public: \
307
using CloneableError<newClass, superClass>::CloneableError; \
308
}
309
310
MakeError(
Error
,
BaseError
);
311
MakeError(
UsageError
,
Error
);
312
MakeError(
UnimplementedError
,
Error
);
313
318
void
throwExceptionSelfCheck();
319
323
[[noreturn]]
324
void
panic(std::string_view msg);
325
334
int
handleExceptions(
const
std::string & programName,
fun
<
void
()> body);
335
341
[[gnu::noinline, gnu::cold, noreturn]]
void
unreachable(std::source_location loc = std::source_location::current());
342
343
#if NIX_UBSAN_ENABLED
344
/* When building with sanitizers, also enable expensive unreachable checks. In
345
optimised builds this explicitly invokes UB with std::unreachable for better
346
optimisations. */
347
# define nixUnreachableWhenHardened ::nix::unreachable
348
#else
349
# define nixUnreachableWhenHardened std::unreachable
350
#endif
351
352
}
// namespace nix
nix::Args
Definition
args.hh:29
nix::BaseError
Definition
error.hh:136
nix::BaseError::calcWhat
const std::string & calcWhat() const
Definition
error.cc:46
nix::BaseError::what_
std::optional< std::string > what_
Definition
error.hh:147
nix::BaseError::addTrace
void addTrace(std::shared_ptr< const Pos > &&pos, std::string_view fs, Args &&... args)
Definition
error.hh:238
nix::BaseError::message
std::string message() const
Definition
error.hh:192
nix::BaseError::unsafeInfo
ErrorInfo & unsafeInfo()
Definition
error.hh:263
nix::CloneableError::throwClone
void throwClone() const override
Definition
error.hh:289
nix::Error
Definition
error.hh:310
nix::HintFmt
Definition
fmt.hh:169
nix::HintFmt::HintFmt
HintFmt(const std::string &literal)
Definition
fmt.hh:178
nix::Suggestions
Definition
suggestions.hh:28
nix::UnimplementedError
Definition
error.hh:312
nix::UsageError
Definition
error.hh:311
nix::fun
Definition
fun.hh:19
nix::TracePrint
TracePrint
Definition
error.hh:90
nix::TracePrint::Always
@ Always
Definition
error.hh:96
nix::TracePrint::Default
@ Default
Definition
error.hh:94
fmt.hh
fun.hh
nix::ErrorInfo
Definition
error.hh:109
nix::ErrorInfo::isFromExpr
bool isFromExpr
Definition
error.hh:118
nix::ErrorInfo::status
unsigned int status
Definition
error.hh:124
nix::LinesOfCode
Definition
error.hh:69
nix::Pos
Definition
position.hh:20
nix::Trace
Definition
error.hh:100
suggestions.hh
libutil
include
nix
util
error.hh
Generated by
1.18.0