2020-05-09 00:18:28 +00:00
|
|
|
#include "error.hh"
|
2020-03-22 18:25:47 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
2020-03-25 16:52:03 +00:00
|
|
|
#include <optional>
|
2020-04-17 21:07:44 +00:00
|
|
|
#include "serialise.hh"
|
2020-05-14 18:28:18 +00:00
|
|
|
#include <sstream>
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2020-04-29 03:06:08 +00:00
|
|
|
namespace nix {
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2020-04-24 20:57:51 +00:00
|
|
|
const std::string nativeSystem = SYSTEM;
|
|
|
|
|
2023-01-18 00:19:07 +00:00
|
|
|
void BaseError::addTrace(std::shared_ptr<AbstractPos> && e, hintformat hint)
|
2020-06-18 21:25:26 +00:00
|
|
|
{
|
2023-01-18 00:19:07 +00:00
|
|
|
err.traces.push_front(Trace { .pos = std::move(e), .hint = hint });
|
2020-06-18 21:25:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:53:19 +00:00
|
|
|
// c++ std::exception descendants must have a 'const char* what()' function.
|
|
|
|
// This stringifies the error and caches it for use by what(), or similarly by msg().
|
2022-02-25 15:00:00 +00:00
|
|
|
const std::string & BaseError::calcWhat() const
|
2020-05-14 18:28:18 +00:00
|
|
|
{
|
|
|
|
if (what_.has_value())
|
|
|
|
return *what_;
|
|
|
|
else {
|
|
|
|
std::ostringstream oss;
|
2021-12-28 12:53:21 +00:00
|
|
|
showErrorInfo(oss, err, loggerSettings.showTrace);
|
2020-05-14 18:28:18 +00:00
|
|
|
what_ = oss.str();
|
|
|
|
return *what_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
std::optional<std::string> ErrorInfo::programName = std::nullopt;
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
std::ostream & operator <<(std::ostream & os, const hintformat & hf)
|
2020-04-15 16:09:43 +00:00
|
|
|
{
|
|
|
|
return os << hf.str();
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
std::ostream & operator <<(std::ostream & str, const AbstractPos & pos)
|
2020-03-22 18:25:47 +00:00
|
|
|
{
|
2022-12-12 23:48:04 +00:00
|
|
|
pos.print(str);
|
|
|
|
str << ":" << pos.line;
|
|
|
|
if (pos.column > 0)
|
|
|
|
str << ":" << pos.column;
|
|
|
|
return str;
|
2020-03-22 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
std::optional<LinesOfCode> AbstractPos::getCodeLines() const
|
2020-05-20 23:25:02 +00:00
|
|
|
{
|
2022-12-12 23:48:04 +00:00
|
|
|
if (line == 0)
|
2020-06-24 14:33:53 +00:00
|
|
|
return std::nullopt;
|
2020-05-20 23:25:02 +00:00
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
if (auto source = getSource()) {
|
|
|
|
|
|
|
|
std::istringstream iss(*source);
|
2020-05-20 23:25:02 +00:00
|
|
|
// count the newlines.
|
|
|
|
int count = 0;
|
2022-12-12 23:48:04 +00:00
|
|
|
std::string curLine;
|
|
|
|
int pl = line - 1;
|
2020-06-24 14:33:53 +00:00
|
|
|
|
|
|
|
LinesOfCode loc;
|
2020-07-06 17:54:53 +00:00
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
do {
|
|
|
|
std::getline(iss, curLine);
|
2020-05-20 23:25:02 +00:00
|
|
|
++count;
|
2020-06-18 21:25:26 +00:00
|
|
|
if (count < pl)
|
|
|
|
;
|
2020-05-20 23:25:02 +00:00
|
|
|
else if (count == pl) {
|
2022-12-12 23:48:04 +00:00
|
|
|
loc.prevLineOfCode = curLine;
|
2020-05-20 23:25:02 +00:00
|
|
|
} else if (count == pl + 1) {
|
2022-12-12 23:48:04 +00:00
|
|
|
loc.errLineOfCode = curLine;
|
2020-05-20 23:25:02 +00:00
|
|
|
} else if (count == pl + 2) {
|
2022-12-12 23:48:04 +00:00
|
|
|
loc.nextLineOfCode = curLine;
|
2020-05-20 23:25:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-21 04:18:26 +00:00
|
|
|
|
|
|
|
if (!iss.good())
|
|
|
|
break;
|
2020-05-20 23:25:02 +00:00
|
|
|
} while (true);
|
2020-06-24 14:33:53 +00:00
|
|
|
|
|
|
|
return loc;
|
2020-05-20 23:25:02 +00:00
|
|
|
}
|
2022-12-12 23:48:04 +00:00
|
|
|
|
|
|
|
return std::nullopt;
|
2020-05-20 23:25:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 04:05:21 +00:00
|
|
|
// print lines of code to the ostream, indicating the error column.
|
2020-10-07 14:33:19 +00:00
|
|
|
void printCodeLines(std::ostream & out,
|
2022-02-25 15:00:00 +00:00
|
|
|
const std::string & prefix,
|
2022-12-12 23:48:04 +00:00
|
|
|
const AbstractPos & errPos,
|
2020-10-07 14:33:19 +00:00
|
|
|
const LinesOfCode & loc)
|
2020-03-22 18:25:47 +00:00
|
|
|
{
|
2020-04-08 15:48:21 +00:00
|
|
|
// previous line of code.
|
2020-06-24 14:33:53 +00:00
|
|
|
if (loc.prevLineOfCode.has_value()) {
|
2020-06-15 12:12:39 +00:00
|
|
|
out << std::endl
|
2020-06-03 20:47:00 +00:00
|
|
|
<< fmt("%1% %|2$5d|| %3%",
|
2020-06-18 21:25:26 +00:00
|
|
|
prefix,
|
2020-06-24 14:33:53 +00:00
|
|
|
(errPos.line - 1),
|
|
|
|
*loc.prevLineOfCode);
|
2020-04-08 15:48:21 +00:00
|
|
|
}
|
2020-04-02 22:02:40 +00:00
|
|
|
|
2020-06-24 14:33:53 +00:00
|
|
|
if (loc.errLineOfCode.has_value()) {
|
2020-05-09 00:18:28 +00:00
|
|
|
// line of code containing the error.
|
2020-06-03 20:47:00 +00:00
|
|
|
out << std::endl
|
|
|
|
<< fmt("%1% %|2$5d|| %3%",
|
2020-06-18 21:25:26 +00:00
|
|
|
prefix,
|
2020-06-24 14:33:53 +00:00
|
|
|
(errPos.line),
|
|
|
|
*loc.errLineOfCode);
|
2020-05-09 00:18:28 +00:00
|
|
|
// error arrows for the column range.
|
2020-06-24 14:33:53 +00:00
|
|
|
if (errPos.column > 0) {
|
|
|
|
int start = errPos.column;
|
2020-05-09 00:18:28 +00:00
|
|
|
std::string spaces;
|
|
|
|
for (int i = 0; i < start; ++i) {
|
|
|
|
spaces.append(" ");
|
|
|
|
}
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2020-05-09 00:18:28 +00:00
|
|
|
std::string arrows("^");
|
2020-04-01 22:20:20 +00:00
|
|
|
|
2020-06-03 20:47:00 +00:00
|
|
|
out << std::endl
|
|
|
|
<< fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL,
|
2020-06-18 21:25:26 +00:00
|
|
|
prefix,
|
|
|
|
spaces,
|
|
|
|
arrows);
|
2020-05-09 00:18:28 +00:00
|
|
|
}
|
2020-03-22 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 15:48:21 +00:00
|
|
|
// next line of code.
|
2020-06-24 14:33:53 +00:00
|
|
|
if (loc.nextLineOfCode.has_value()) {
|
2020-06-03 20:47:00 +00:00
|
|
|
out << std::endl
|
|
|
|
<< fmt("%1% %|2$5d|| %3%",
|
2020-06-18 21:25:26 +00:00
|
|
|
prefix,
|
2020-06-24 14:33:53 +00:00
|
|
|
(errPos.line + 1),
|
|
|
|
*loc.nextLineOfCode);
|
2020-04-08 15:48:21 +00:00
|
|
|
}
|
2020-03-22 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 23:27:36 +00:00
|
|
|
static std::string indent(std::string_view indentFirst, std::string_view indentRest, std::string_view s)
|
2020-03-22 18:25:47 +00:00
|
|
|
{
|
2021-01-20 23:27:36 +00:00
|
|
|
std::string res;
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
while (!s.empty()) {
|
|
|
|
auto end = s.find('\n');
|
|
|
|
if (!first) res += "\n";
|
2021-01-20 23:49:29 +00:00
|
|
|
res += chomp(std::string(first ? indentFirst : indentRest) + std::string(s.substr(0, end)));
|
2021-01-20 23:27:36 +00:00
|
|
|
first = false;
|
|
|
|
if (end == s.npos) break;
|
|
|
|
s = s.substr(end + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2021-01-20 23:27:36 +00:00
|
|
|
std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool showTrace)
|
|
|
|
{
|
|
|
|
std::string prefix;
|
2020-04-02 22:02:40 +00:00
|
|
|
switch (einfo.level) {
|
2020-05-09 00:18:28 +00:00
|
|
|
case Verbosity::lvlError: {
|
2021-01-20 23:27:36 +00:00
|
|
|
prefix = ANSI_RED "error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlNotice: {
|
|
|
|
prefix = ANSI_RED "note";
|
2020-05-09 00:18:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlWarn: {
|
2021-09-14 08:38:10 +00:00
|
|
|
prefix = ANSI_WARNING "warning";
|
2020-05-09 00:18:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlInfo: {
|
2021-01-20 23:27:36 +00:00
|
|
|
prefix = ANSI_GREEN "info";
|
2020-05-09 00:18:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlTalkative: {
|
2021-01-20 23:27:36 +00:00
|
|
|
prefix = ANSI_GREEN "talk";
|
2020-05-09 00:18:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlChatty: {
|
2021-01-20 23:27:36 +00:00
|
|
|
prefix = ANSI_GREEN "chat";
|
2020-05-09 00:18:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlVomit: {
|
2021-01-20 23:27:36 +00:00
|
|
|
prefix = ANSI_GREEN "vomit";
|
2020-05-09 00:18:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlDebug: {
|
2021-09-14 08:38:10 +00:00
|
|
|
prefix = ANSI_WARNING "debug";
|
2020-05-09 00:18:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-01-20 23:27:36 +00:00
|
|
|
default:
|
|
|
|
assert(false);
|
2020-03-22 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 23:27:36 +00:00
|
|
|
// FIXME: show the program name as part of the trace?
|
|
|
|
if (einfo.programName && einfo.programName != ErrorInfo::programName)
|
|
|
|
prefix += fmt(" [%s]:" ANSI_NORMAL " ", einfo.programName.value_or(""));
|
2020-04-25 18:05:26 +00:00
|
|
|
else
|
2021-01-20 23:27:36 +00:00
|
|
|
prefix += ":" ANSI_NORMAL " ";
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2021-01-20 23:27:36 +00:00
|
|
|
std::ostringstream oss;
|
2020-06-18 21:25:26 +00:00
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
auto noSource = ANSI_ITALIC " (source not available)" ANSI_NORMAL "\n";
|
2022-03-03 09:50:35 +00:00
|
|
|
|
2023-01-18 00:19:07 +00:00
|
|
|
// traces
|
|
|
|
if (showTrace && !einfo.traces.empty()) {
|
2022-12-09 17:36:25 +00:00
|
|
|
for (const auto & trace : einfo.traces) {
|
|
|
|
oss << "\n" << "… " << trace.hint.str() << "\n";
|
2020-07-06 16:51:48 +00:00
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
if (trace.pos) {
|
2023-01-18 00:19:07 +00:00
|
|
|
oss << "\n" << ANSI_BLUE << "at " ANSI_WARNING << *trace.pos << ANSI_NORMAL << ":";
|
2022-11-22 16:45:58 +00:00
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
if (auto loc = trace.pos->getCodeLines()) {
|
2022-11-22 16:45:58 +00:00
|
|
|
oss << "\n";
|
2022-12-12 23:48:04 +00:00
|
|
|
printCodeLines(oss, "", *trace.pos, *loc);
|
2021-01-20 23:27:36 +00:00
|
|
|
oss << "\n";
|
2022-12-12 23:48:04 +00:00
|
|
|
} else
|
|
|
|
oss << noSource;
|
2020-06-25 00:31:28 +00:00
|
|
|
}
|
2020-06-23 15:36:58 +00:00
|
|
|
}
|
2022-11-22 16:45:58 +00:00
|
|
|
oss << "\n" << prefix;
|
|
|
|
}
|
|
|
|
|
2021-01-20 23:27:36 +00:00
|
|
|
oss << einfo.msg << "\n";
|
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
if (einfo.errPos) {
|
|
|
|
oss << "\n" << ANSI_BLUE << "at " ANSI_WARNING << *einfo.errPos << ANSI_NORMAL << ":";
|
2020-06-18 21:25:26 +00:00
|
|
|
|
2022-12-12 23:48:04 +00:00
|
|
|
if (auto loc = einfo.errPos->getCodeLines()) {
|
2021-01-20 23:27:36 +00:00
|
|
|
oss << "\n";
|
|
|
|
printCodeLines(oss, "", *einfo.errPos, *loc);
|
|
|
|
oss << "\n";
|
2022-12-12 23:48:04 +00:00
|
|
|
} else
|
|
|
|
oss << noSource;
|
2020-05-21 20:28:45 +00:00
|
|
|
}
|
2020-05-20 23:25:02 +00:00
|
|
|
|
2022-03-03 09:50:35 +00:00
|
|
|
auto suggestions = einfo.suggestions.trim();
|
2022-12-12 23:48:04 +00:00
|
|
|
if (!suggestions.suggestions.empty()) {
|
2022-03-03 09:50:35 +00:00
|
|
|
oss << "Did you mean " <<
|
2022-03-07 09:04:57 +00:00
|
|
|
suggestions.trim() <<
|
2022-03-03 09:50:35 +00:00
|
|
|
"?" << std::endl;
|
2020-06-23 15:36:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 23:27:36 +00:00
|
|
|
out << indent(prefix, std::string(filterANSIEscapes(prefix, true).size(), ' '), chomp(oss.str()));
|
|
|
|
|
2020-04-17 21:07:44 +00:00
|
|
|
return out;
|
|
|
|
}
|
2020-03-22 18:25:47 +00:00
|
|
|
}
|