Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
error.hh
Go to the documentation of this file.
1#pragma once
17
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
33namespace nix {
34
35enum 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. */
47using enum Verbosity;
48
49template<std::integral T>
50 requires(std::is_unsigned_v<T> && !std::is_same_v<bool, T>)
51inline 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. */
61template<std::integral T>
62 requires std::is_signed_v<T>
63Verbosity verbosityFromIntClamped(T) = delete;
64
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? */
83struct Pos;
84
85void printCodeLines(std::ostream & out, const std::string & prefix, const Pos & errPos, const LinesOfCode & loc);
86
90enum struct TracePrint {
97};
98
99struct Trace
100{
101 std::shared_ptr<const Pos> pos;
102 HintFmt hint;
104};
105
106inline std::strong_ordering operator<=>(const Trace & lhs, const Trace & rhs);
107
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
129std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool showTrace);
130
135class 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
141protected:
142 mutable ErrorInfo err;
143
147 mutable std::optional<std::string> what_;
151 const std::string & calcWhat() const;
152
153public:
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
264 {
265 return err;
266 }
267
272 void recalcWhat() const;
273
274 [[noreturn]] virtual void throwClone() const = 0;
275};
276
277template<typename Derived, typename Base>
278class CloneableError : public Base
279{
280 friend Derived;
281 CloneableError() = default;
282 using Base::Base;
283public:
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
310MakeError(Error, BaseError);
311MakeError(UsageError, Error);
313
318void throwExceptionSelfCheck();
319
323[[noreturn]]
324void panic(std::string_view msg);
325
334int 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
Definition args.hh:29
Definition error.hh:136
const std::string & calcWhat() const
Definition error.cc:46
std::optional< std::string > what_
Definition error.hh:147
void addTrace(std::shared_ptr< const Pos > &&pos, std::string_view fs, Args &&... args)
Definition error.hh:238
std::string message() const
Definition error.hh:192
ErrorInfo & unsafeInfo()
Definition error.hh:263
void throwClone() const override
Definition error.hh:289
Definition error.hh:310
Definition fmt.hh:169
HintFmt(const std::string &literal)
Definition fmt.hh:178
Definition suggestions.hh:28
Definition error.hh:312
Definition error.hh:311
Definition fun.hh:19
TracePrint
Definition error.hh:90
@ Always
Definition error.hh:96
@ Default
Definition error.hh:94
Definition error.hh:109
bool isFromExpr
Definition error.hh:118
unsigned int status
Definition error.hh:124
Definition error.hh:69
Definition position.hh:20
Definition error.hh:100