diff options
author | Guillaume Maudoux <guillaume.maudoux@tweag.io> | 2022-03-18 00:58:09 +0100 |
---|---|---|
committer | Guillaume Maudoux <guillaume.maudoux@tweag.io> | 2022-03-18 00:58:09 +0100 |
commit | e6d07e0d89d964cde22894fca57a95177c085c8d (patch) | |
tree | 90d2d64ea4f2796aadce2853ed4da31e0d46b047 /src/libutil | |
parent | 13c4dc65327c9654c47e6d80c0f4e1797b999f97 (diff) |
Refactor to use more traces and less string manipulations
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/error.cc | 23 | ||||
-rw-r--r-- | src/libutil/error.hh | 10 |
2 files changed, 23 insertions, 10 deletions
diff --git a/src/libutil/error.cc b/src/libutil/error.cc index dcd2f82a5..134b99893 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -185,15 +185,15 @@ void printAtPos(const ErrPos & pos, std::ostream & out) if (pos) { switch (pos.origin) { case foFile: { - out << fmt(ANSI_BLUE "at " ANSI_WARNING "%s:%s" ANSI_NORMAL ":", pos.file, showErrPos(pos)); + out << fmt(ANSI_BLUE " at " ANSI_WARNING "%s:%s" ANSI_NORMAL ":", pos.file, showErrPos(pos)); break; } case foString: { - out << fmt(ANSI_BLUE "at " ANSI_WARNING "«string»:%s" ANSI_NORMAL ":", showErrPos(pos)); + out << fmt(ANSI_BLUE " at " ANSI_WARNING "«string»:%s" ANSI_NORMAL ":", showErrPos(pos)); break; } case foStdin: { - out << fmt(ANSI_BLUE "at " ANSI_WARNING "«stdin»:%s" ANSI_NORMAL ":", showErrPos(pos)); + out << fmt(ANSI_BLUE " at " ANSI_WARNING "«stdin»:%s" ANSI_NORMAL ":", showErrPos(pos)); break; } default: @@ -269,7 +269,6 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s oss << einfo.msg << "\n"; if (einfo.errPos.has_value() && *einfo.errPos) { - oss << "\n"; printAtPos(*einfo.errPos, oss); auto loc = getCodeLines(*einfo.errPos); @@ -278,26 +277,34 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s if (loc.has_value()) { oss << "\n"; printCodeLines(oss, "", *einfo.errPos, *loc); - oss << "\n"; } + oss << "\n"; } // traces - if (showTrace && !einfo.traces.empty()) { + if (!einfo.traces.empty()) { + unsigned int count = 0; for (auto iter = einfo.traces.rbegin(); iter != einfo.traces.rend(); ++iter) { + if (!showTrace && count > 3) { + oss << "\n" << "(truncated)" << "\n"; + break; + } + + if (iter->hint.str().empty()) continue; + count++; oss << "\n" << "… " << iter->hint.str() << "\n"; if (iter->pos.has_value() && (*iter->pos)) { + count++; auto pos = iter->pos.value(); - oss << "\n"; printAtPos(pos, oss); auto loc = getCodeLines(pos); if (loc.has_value()) { oss << "\n"; printCodeLines(oss, "", pos, *loc); - oss << "\n"; } + oss << "\n"; } } } diff --git a/src/libutil/error.hh b/src/libutil/error.hh index d55e1d701..ad29f8d2a 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -130,6 +130,8 @@ protected: public: unsigned int status = 1; // exit status + BaseError(const BaseError &) = default; + template<typename... Args> BaseError(unsigned int status, const Args & ... args) : err { .level = lvlError, .msg = hintfmt(args...) } @@ -165,10 +167,14 @@ public: const std::string & msg() const { return calcWhat(); } const ErrorInfo & info() const { calcWhat(); return err; } + void pushTrace(Trace trace) { + err.traces.push_front(trace); + } + template<typename... Args> - BaseError & addTrace(std::optional<ErrPos> e, const std::string & fs, const Args & ... args) + BaseError & addTrace(std::optional<ErrPos> e, const std::string_view & fs, const Args & ... args) { - return addTrace(e, hintfmt(fs, args...)); + return addTrace(e, hintfmt(std::string(fs), args...)); } BaseError & addTrace(std::optional<ErrPos> e, hintformat hint); |