1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-26 15:51:15 +02:00

addErrorTrace

This commit is contained in:
Ben Burdette 2020-06-19 13:44:08 -06:00
parent 4d1a4f0217
commit 54e8f550c9
8 changed files with 270 additions and 220 deletions

View file

@ -12,20 +12,43 @@ const std::string nativeSystem = SYSTEM;
// addPrefix is used for show-trace. Strings added with addPrefix
// will print ahead of the error itself.
BaseError & BaseError::addPrefix(const FormatOrString & fs)
{
prefix_ = fs.s + prefix_;
return *this;
}
// BaseError & BaseError::addPrefix(const FormatOrString & fs)
// {
// prefix_ = fs.s + prefix_;
// return *this;
// }
// const string & prefix() const
// {
// // build prefix string on demand??
// }
// ; // { return prefix_; }
// addPrefix is used for show-trace. Strings added with addPrefix
// will print ahead of the error itself.
BaseError & BaseError::addTrace(hintformat hint, ErrPos e)
BaseError & BaseError::addTrace(std::optional<ErrPos> e, hintformat hint)
{
err.traces.push_front(Trace { .pos = e, .hint = hint});
return *this;
}
// const string& BaseError::calcTrace() const
// {
// if (trace_.has_value())
// return *trace_;
// else {
// err.name = sname();
// std::ostringstream oss;
// oss << err;
// trace_ = oss.str();
// return *trace_;
// }
// }
// 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().
const string& BaseError::calcWhat() const
@ -284,6 +307,22 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
nl = true;
}
// traces
for (auto iter = einfo.traces.begin(); iter != einfo.traces.end(); ++iter)
{
try {
auto pos = *iter->pos;
out << iter->hint.str() << showErrPos(pos) << std::endl;
NixCode nc { .errPos = pos };
getCodeLines(nc);
printCodeLines(out, prefix, nc);
} catch(const std::bad_optional_access& e) {
out << iter->hint.str() << std::endl;
}
}
// description
if (einfo.description != "") {
if (nl)