1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-30 11:43: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)

View file

@ -94,7 +94,7 @@ struct NixCode {
};
struct Trace {
ErrPos pos;
std::optional<ErrPos> pos;
hintformat hint;
};
@ -116,9 +116,12 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo);
class BaseError : public std::exception
{
protected:
string prefix_; // used for location traces etc.
// string prefix_; // used for location traces etc.
mutable ErrorInfo err;
// mutable std::optional<string> trace_;
// const string& calcTrace() const;
mutable std::optional<string> what_;
const string& calcWhat() const;
@ -164,9 +167,9 @@ public:
#endif
const string & msg() const { return calcWhat(); }
const string & prefix() const { return prefix_; }
BaseError & addPrefix(const FormatOrString & fs);
BaseError & addTrace(ErrPos e, hintformat hint);
// const string & trace() const { return calcTrace(); }
// BaseError & addPrefix(const FormatOrString & fs);
BaseError & addTrace(std::optional<ErrPos> e, hintformat hint);
const ErrorInfo & info() { calcWhat(); return err; }
};