This commit is contained in:
Ben Burdette 2020-06-18 15:25:26 -06:00
parent e6f93b94fc
commit 4d1a4f0217
3 changed files with 59 additions and 44 deletions

View file

@ -239,13 +239,13 @@ void prim_exec(EvalState & state, const Pos & pos, Value * * args, Value & v)
try {
parsed = state.parseExprFromString(output, pos.file);
} catch (Error & e) {
e.addPrefix(fmt("While parsing the output from '%1%', at %2%\n", program, pos));
e.addTrace(pos, hintfmt("While parsing the output from '%1%'", program));
throw;
}
try {
state.eval(parsed, v);
} catch (Error & e) {
e.addPrefix(fmt("While evaluating the output from '%1%', at %2%\n", program, pos));
e.addTrace(pos, hintfmt("While evaluating the output from '%1%'", program));
throw;
}
}

View file

@ -18,6 +18,14 @@ BaseError & BaseError::addPrefix(const FormatOrString & fs)
return *this;
}
// addPrefix is used for show-trace. Strings added with addPrefix
// will print ahead of the error itself.
BaseError & BaseError::addTrace(hintformat hint, ErrPos e)
{
err.traces.push_front(Trace { .pos = e, .hint = hint});
return *this;
}
// 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

View file

@ -93,12 +93,18 @@ struct NixCode {
std::optional<string> nextLineOfCode;
};
struct Trace {
ErrPos pos;
hintformat hint;
};
struct ErrorInfo {
Verbosity level;
string name;
string description;
std::optional<hintformat> hint;
std::optional<NixCode> nixCode;
std::list<Trace> traces;
static std::optional<string> programName;
};
@ -160,6 +166,7 @@ public:
const string & msg() const { return calcWhat(); }
const string & prefix() const { return prefix_; }
BaseError & addPrefix(const FormatOrString & fs);
BaseError & addTrace(ErrPos e, hintformat hint);
const ErrorInfo & info() { calcWhat(); return err; }
};