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 { try {
parsed = state.parseExprFromString(output, pos.file); parsed = state.parseExprFromString(output, pos.file);
} catch (Error & e) { } 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; throw;
} }
try { try {
state.eval(parsed, v); state.eval(parsed, v);
} catch (Error & e) { } 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; throw;
} }
} }

View file

@ -18,6 +18,14 @@ BaseError & BaseError::addPrefix(const FormatOrString & fs)
return *this; 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. // 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(). // This stringifies the error and caches it for use by what(), or similarly by msg().
const string& BaseError::calcWhat() const const string& BaseError::calcWhat() const

View file

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