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