Nix 2.26.3
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
18#include "suggestions.hh"
19#include "fmt.hh"
20
21#include <cstring>
22#include <list>
23#include <memory>
24#include <optional>
25
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29
30namespace nix {
31
32
33typedef enum {
34 lvlError = 0,
35 lvlWarn,
36 lvlNotice,
37 lvlInfo,
38 lvlTalkative,
39 lvlChatty,
40 lvlDebug,
41 lvlVomit
42} Verbosity;
43
48 std::optional<std::string> prevLineOfCode;
49 std::optional<std::string> errLineOfCode;
50 std::optional<std::string> nextLineOfCode;
51};
52
53struct Pos;
54
55void printCodeLines(std::ostream & out,
56 const std::string & prefix,
57 const Pos & errPos,
58 const LinesOfCode & loc);
59
63enum struct TracePrint {
70};
71
72struct Trace {
73 std::shared_ptr<Pos> pos;
74 HintFmt hint;
76};
77
78inline std::strong_ordering operator<=>(const Trace& lhs, const Trace& rhs);
79
80struct ErrorInfo {
81 Verbosity level;
82 HintFmt msg;
83 std::shared_ptr<Pos> pos;
84 std::list<Trace> traces;
89 bool isFromExpr = false;
90
94 unsigned int status = 1;
95
96 Suggestions suggestions;
97
98 static std::optional<std::string> programName;
99};
100
101std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool showTrace);
102
107class BaseError : public std::exception
108{
109protected:
110 mutable ErrorInfo err;
111
115 mutable std::optional<std::string> what_;
119 const std::string & calcWhat() const;
120
121public:
122 BaseError(const BaseError &) = default;
123 BaseError& operator=(const BaseError &) = default;
124 BaseError& operator=(BaseError &&) = default;
125
126 template<typename... Args>
127 BaseError(unsigned int status, const Args & ... args)
128 : err { .level = lvlError, .msg = HintFmt(args...), .status = status }
129 { }
130
131 template<typename... Args>
132 explicit BaseError(const std::string & fs, const Args & ... args)
133 : err { .level = lvlError, .msg = HintFmt(fs, args...) }
134 { }
135
136 template<typename... Args>
137 BaseError(const Suggestions & sug, const Args & ... args)
138 : err { .level = lvlError, .msg = HintFmt(args...), .suggestions = sug }
139 { }
140
141 BaseError(HintFmt hint)
142 : err { .level = lvlError, .msg = hint }
143 { }
144
145 BaseError(ErrorInfo && e)
146 : err(std::move(e))
147 { }
148
149 BaseError(const ErrorInfo & e)
150 : err(e)
151 { }
152
154 std::string message() {
155 return err.msg.str();
156 }
157
158 const char * what() const noexcept override { return calcWhat().c_str(); }
159 const std::string & msg() const { return calcWhat(); }
160 const ErrorInfo & info() const { calcWhat(); return err; }
161
162 void withExitStatus(unsigned int status)
163 {
164 err.status = status;
165 }
166
167 void atPos(std::shared_ptr<Pos> pos) {
168 err.pos = pos;
169 }
170
171 void pushTrace(Trace trace)
172 {
173 err.traces.push_front(trace);
174 }
175
176 template<typename... Args>
177 void addTrace(std::shared_ptr<Pos> && e, std::string_view fs, const Args & ... args)
178 {
179 addTrace(std::move(e), HintFmt(std::string(fs), args...));
180 }
181
182 void addTrace(std::shared_ptr<Pos> && e, HintFmt hint, TracePrint print = TracePrint::Default);
183
184 bool hasTrace() const { return !err.traces.empty(); }
185
186 const ErrorInfo & info() { return err; };
187};
188
189#define MakeError(newClass, superClass) \
190 class newClass : public superClass \
191 { \
192 public: \
193 using superClass::superClass; \
194 }
195
196MakeError(Error, BaseError);
197MakeError(UsageError, Error);
198MakeError(UnimplementedError, Error);
199
203MakeError(SystemError, Error);
204
221class SysError : public SystemError
222{
223public:
224 int errNo;
225
230 template<typename... Args>
231 SysError(int errNo, const Args & ... args)
232 : SystemError(""), errNo(errNo)
233 {
234 auto hf = HintFmt(args...);
235 err.msg = HintFmt("%1%: %2%", Uncolored(hf.str()), strerror(errNo));
236 }
237
244 template<typename... Args>
245 SysError(const Args & ... args)
246 : SysError(errno, args ...)
247 {
248 }
249};
250
251#ifdef _WIN32
252namespace windows {
253 class WinError;
254}
255#endif
256
263#ifdef _WIN32
264 windows::WinError
265#else
267#endif
268 ;
269
274void throwExceptionSelfCheck();
275
279[[noreturn]]
280void panic(std::string_view msg);
281
286[[noreturn]]
287void panic(const char * file, int line, const char * func);
288
294#define unreachable() (::nix::panic(__FILE__, __LINE__, __func__))
295
296}
Definition args.hh:28
Definition error.hh:108
const std::string & calcWhat() const
Definition error.cc:29
std::optional< std::string > what_
Definition error.hh:115
std::string message()
Definition error.hh:154
Definition fmt.hh:136
Definition suggestions.hh:26
Definition error.hh:222
SysError(const Args &... args)
Definition error.hh:245
SysError(int errNo, const Args &... args)
Definition error.hh:231
SysError NativeSysError
Definition error.hh:262
TracePrint
Definition error.hh:63
@ Always
Definition error.hh:69
@ Default
Definition error.hh:67
bool addTrace
Definition lexer.l:7062
uint32_t line
Definition lexer.l:6526
void panic()
ErrorInfo err
Definition lexer.l:679
std::vector< Expr * > args
Definition lexer.l:6126
unsigned int status
Definition lexer.l:663
Definition error.hh:80
bool isFromExpr
Definition error.hh:89
unsigned int status
Definition error.hh:94
Definition error.hh:47
Definition position.hh:20
Definition error.hh:72
Definition fmt.hh:120