forked from lix-project/lix
addTrace
This commit is contained in:
parent
e6f93b94fc
commit
4d1a4f0217
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -25,20 +25,20 @@ namespace nix {
|
|||
|
||||
/*
|
||||
|
||||
This file defines two main structs/classes used in nix error handling.
|
||||
This file defines two main structs/classes used in nix error handling.
|
||||
|
||||
ErrorInfo provides a standard payload of error information, with conversion to string
|
||||
happening in the logger rather than at the call site.
|
||||
ErrorInfo provides a standard payload of error information, with conversion to string
|
||||
happening in the logger rather than at the call site.
|
||||
|
||||
BaseError is the ancestor of nix specific exceptions (and Interrupted), and contains
|
||||
an ErrorInfo.
|
||||
BaseError is the ancestor of nix specific exceptions (and Interrupted), and contains
|
||||
an ErrorInfo.
|
||||
|
||||
ErrorInfo structs are sent to the logger as part of an exception, or directly with the
|
||||
logError or logWarning macros.
|
||||
ErrorInfo structs are sent to the logger as part of an exception, or directly with the
|
||||
logError or logWarning macros.
|
||||
|
||||
See the error-demo.cc program for usage examples.
|
||||
See the error-demo.cc program for usage examples.
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
lvlError = 0,
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -121,7 +127,7 @@ public:
|
|||
|
||||
template<typename... Args>
|
||||
BaseError(unsigned int status, const Args & ... args)
|
||||
: err { .level = lvlError,
|
||||
: err {.level = lvlError,
|
||||
.hint = hintfmt(args...)
|
||||
}
|
||||
, status(status)
|
||||
|
@ -129,13 +135,13 @@ public:
|
|||
|
||||
template<typename... Args>
|
||||
BaseError(const std::string & fs, const Args & ... args)
|
||||
: err { .level = lvlError,
|
||||
: err {.level = lvlError,
|
||||
.hint = hintfmt(fs, args...)
|
||||
}
|
||||
{ }
|
||||
|
||||
BaseError(hintformat hint)
|
||||
: err { .level = lvlError,
|
||||
: err {.level = lvlError,
|
||||
.hint = hint
|
||||
}
|
||||
{ }
|
||||
|
@ -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; }
|
||||
};
|
||||
|
@ -181,7 +188,7 @@ public:
|
|||
|
||||
template<typename... Args>
|
||||
SysError(const Args & ... args)
|
||||
:Error("")
|
||||
: Error("")
|
||||
{
|
||||
errNo = errno;
|
||||
auto hf = hintfmt(args...);
|
||||
|
|
Loading…
Reference in a new issue