diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2023-01-02 20:53:39 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2023-01-02 20:53:39 +0100 |
commit | 6b6965238506705382892b14ef22700fc5112c3d (patch) | |
tree | c5a0f04bee2bace1281d7c7b405c8214641072c8 /src/libutil/error.cc | |
parent | e0ab2069c975abf80db9fdca23f0c164df921829 (diff) | |
parent | 9af16c5f742300e831a2cc400e43df1e22f87f31 (diff) |
Merge remote-tracking branch 'origin/master' into coerce-string
Diffstat (limited to 'src/libutil/error.cc')
-rw-r--r-- | src/libutil/error.cc | 193 |
1 files changed, 65 insertions, 128 deletions
diff --git a/src/libutil/error.cc b/src/libutil/error.cc index cf4f4a56f..7f7c27267 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -9,9 +9,9 @@ namespace nix { const std::string nativeSystem = SYSTEM; -void BaseError::addTrace(std::optional<ErrPos> e, hintformat hint, bool frame) +void BaseError::addTrace(std::shared_ptr<AbstractPos> && e, hintformat hint, bool frame) { - err.traces.push_front(Trace { .pos = e, .hint = hint, .frame = frame }); + err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .frame = frame }); } // c++ std::exception descendants must have a 'const char* what()' function. @@ -30,91 +30,46 @@ const std::string & BaseError::calcWhat() const std::optional<std::string> ErrorInfo::programName = std::nullopt; -std::ostream & operator<<(std::ostream & os, const hintformat & hf) +std::ostream & operator <<(std::ostream & os, const hintformat & hf) { return os << hf.str(); } -std::string showErrPos(const ErrPos & errPos) +std::ostream & operator <<(std::ostream & str, const AbstractPos & pos) { - if (errPos.line > 0) { - if (errPos.column > 0) { - return fmt("%d:%d", errPos.line, errPos.column); - } else { - return fmt("%d", errPos.line); - } - } - else { - return ""; - } + pos.print(str); + str << ":" << pos.line; + if (pos.column > 0) + str << ":" << pos.column; + return str; } -std::optional<LinesOfCode> getCodeLines(const ErrPos & errPos) +std::optional<LinesOfCode> AbstractPos::getCodeLines() const { - if (errPos.line <= 0) + if (line == 0) return std::nullopt; - if (errPos.origin == foFile) { - LinesOfCode loc; - try { - // FIXME: when running as the daemon, make sure we don't - // open a file to which the client doesn't have access. - AutoCloseFD fd = open(errPos.file.c_str(), O_RDONLY | O_CLOEXEC); - if (!fd) return {}; - - // count the newlines. - int count = 0; - std::string line; - int pl = errPos.line - 1; - do - { - line = readLine(fd.get()); - ++count; - if (count < pl) - ; - else if (count == pl) - loc.prevLineOfCode = line; - else if (count == pl + 1) - loc.errLineOfCode = line; - else if (count == pl + 2) { - loc.nextLineOfCode = line; - break; - } - } while (true); - return loc; - } - catch (EndOfFile & eof) { - if (loc.errLineOfCode.has_value()) - return loc; - else - return std::nullopt; - } - catch (std::exception & e) { - return std::nullopt; - } - } else { - std::istringstream iss(errPos.file); + if (auto source = getSource()) { + + std::istringstream iss(*source); // count the newlines. int count = 0; - std::string line; - int pl = errPos.line - 1; + std::string curLine; + int pl = line - 1; LinesOfCode loc; - do - { - std::getline(iss, line); + do { + std::getline(iss, curLine); ++count; if (count < pl) - { ; - } else if (count == pl) { - loc.prevLineOfCode = line; + loc.prevLineOfCode = curLine; } else if (count == pl + 1) { - loc.errLineOfCode = line; + loc.errLineOfCode = curLine; } else if (count == pl + 2) { - loc.nextLineOfCode = line; + loc.nextLineOfCode = curLine; break; } @@ -124,12 +79,14 @@ std::optional<LinesOfCode> getCodeLines(const ErrPos & errPos) return loc; } + + return std::nullopt; } // print lines of code to the ostream, indicating the error column. void printCodeLines(std::ostream & out, const std::string & prefix, - const ErrPos & errPos, + const AbstractPos & errPos, const LinesOfCode & loc) { // previous line of code. @@ -176,32 +133,6 @@ void printCodeLines(std::ostream & out, } } -// Enough indent to align with with the `... ` -// prepended to each element of the trace -#define ELLIPSIS_INDENT " " - -void printAtPos(const ErrPos & pos, std::ostream & out) -{ - if (pos) { - switch (pos.origin) { - case foFile: { - out << fmt(ELLIPSIS_INDENT "at " ANSI_WARNING "%s:%s" ANSI_NORMAL ":", pos.file, showErrPos(pos)); - break; - } - case foString: { - out << fmt(ELLIPSIS_INDENT "at " ANSI_WARNING "«string»:%s" ANSI_NORMAL ":", showErrPos(pos)); - break; - } - case foStdin: { - out << fmt(ELLIPSIS_INDENT "at " ANSI_WARNING "«stdin»:%s" ANSI_NORMAL ":", showErrPos(pos)); - break; - } - default: - throw Error("invalid FileOrigin in errPos"); - } - } -} - static std::string indent(std::string_view indentFirst, std::string_view indentRest, std::string_view s) { std::string res; @@ -266,27 +197,8 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s prefix += ":" ANSI_NORMAL " "; std::ostringstream oss; - oss << einfo.msg << "\n"; - - if (einfo.errPos.has_value() && *einfo.errPos) { - printAtPos(*einfo.errPos, oss); - - auto loc = getCodeLines(*einfo.errPos); - // lines of code. - if (loc.has_value()) { - oss << "\n"; - printCodeLines(oss, "", *einfo.errPos, *loc); - } - oss << "\n"; - } - - auto suggestions = einfo.suggestions.trim(); - if (! suggestions.suggestions.empty()){ - oss << "Did you mean " << - suggestions.trim() << - "?" << std::endl; - } + auto noSource = ANSI_ITALIC " (source not available)" ANSI_NORMAL "\n"; /* * Traces @@ -382,36 +294,61 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s * */ + // Enough indent to align with with the `... ` + // prepended to each element of the trace + auto ellipsisIndent = " "; + bool frameOnly = false; if (!einfo.traces.empty()) { - unsigned int count = 0; - for (auto iter = einfo.traces.rbegin(); iter != einfo.traces.rend(); ++iter) { + size_t count = 0; + for (const auto & trace : einfo.traces) { if (!showTrace && count > 3) { - oss << "\n" << "(truncated)" << "\n"; + oss << "\n" << ANSI_ITALIC "(stack trace truncated)" ANSI_NORMAL << "\n"; break; } - if (iter->hint.str().empty()) continue; - if (frameOnly && !iter->frame) continue; + if (trace.hint.str().empty()) continue; + if (frameOnly && !trace.frame) continue; count++; - frameOnly = iter->frame; + frameOnly = trace.frame; - oss << "\n" << "… " << iter->hint.str() << "\n"; + oss << "\n" << "… " << trace.hint.str() << "\n"; - if (iter->pos.has_value() && (*iter->pos)) { + if (trace.pos) { count++; - auto pos = iter->pos.value(); - printAtPos(pos, oss); - auto loc = getCodeLines(pos); - if (loc.has_value()) { + oss << "\n" << ellipsisIndent << ANSI_BLUE << "at " ANSI_WARNING << *trace.pos << ANSI_NORMAL << ":"; + + if (auto loc = trace.pos->getCodeLines()) { + oss << "\n"; + printCodeLines(oss, "", *trace.pos, *loc); oss << "\n"; - printCodeLines(oss, "", pos, *loc); - } - oss << "\n"; + } else + oss << noSource; } } + oss << "\n" << prefix; + } + + oss << einfo.msg << "\n"; + + if (einfo.errPos) { + oss << "\n" << ANSI_BLUE << "at " ANSI_WARNING << *einfo.errPos << ANSI_NORMAL << ":"; + + if (auto loc = einfo.errPos->getCodeLines()) { + oss << "\n"; + printCodeLines(oss, "", *einfo.errPos, *loc); + oss << "\n"; + } else + oss << noSource; + } + + auto suggestions = einfo.suggestions.trim(); + if (!suggestions.suggestions.empty()) { + oss << "Did you mean " << + suggestions.trim() << + "?" << std::endl; } out << indent(prefix, std::string(filterANSIEscapes(prefix, true).size(), ' '), chomp(oss.str())); |