position for stdin, string; (string) for trace; fix tests

This commit is contained in:
Ben Burdette 2020-05-21 14:28:45 -06:00
parent 6a420d672c
commit 0e49de6a2b
5 changed files with 60 additions and 38 deletions

View file

@ -17,7 +17,8 @@ makefiles = \
misc/upstart/local.mk \
doc/manual/local.mk \
tests/local.mk \
tests/plugins/local.mk
tests/plugins/local.mk \
src/error-demo/local.mk
-include Makefile.config

View file

@ -118,7 +118,7 @@ int main()
"yellow",
"values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13),
.errPos = Pos(foFile, problem_file, 40, 13),
.prevLineOfCode = std::nullopt,
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = std::nullopt
@ -132,7 +132,7 @@ int main()
"yellow",
"values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13),
.errPos = Pos(foFile, problem_file, 40, 13),
.prevLineOfCode = "previous line of code",
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = "next line of code",
@ -147,7 +147,7 @@ int main()
"yellow",
"values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13)
.errPos = Pos(foFile, problem_file, 40, 13)
}});
// Error with only hint and name..
@ -155,7 +155,7 @@ int main()
ErrorInfo { .name = "error name",
.hint = hintfmt("hint %1%", "only"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13)
.errPos = Pos(foFile, problem_file, 40, 13)
}});
return 0;

View file

@ -197,7 +197,22 @@ std::ostream & operator << (std::ostream & str, const Pos & pos)
if (!pos)
str << "undefined position";
else
str << (format(ANSI_BOLD "%1%" ANSI_NORMAL ":%2%:%3%") % (string) pos.file % pos.line % pos.column).str();
{
auto f = format(ANSI_BOLD "%1%" ANSI_NORMAL ":%2%:%3%");
switch (pos.origin) {
case foFile:
f % (string) pos.file;
break;
case foStdin:
case foString:
f % "(string)";
break;
default:
throw Error("unhandled Pos origin!");
}
str << (f % pos.line % pos.column).str();
}
return str;
}

View file

@ -58,34 +58,35 @@ void getCodeLines(NixCode &nixCode)
if (nixCode.errPos.line <= 0)
return;
// check this magic value!
if (nixCode.errPos.origin == foFile) {
try {
AutoCloseFD fd = open(nixCode.errPos.file.c_str(), O_RDONLY | O_CLOEXEC);
if (!fd)
throw SysError("opening file '%1%'", nixCode.errPos.file);
// count the newlines.
int count = 0;
string line;
int pl = nixCode.errPos.line - 1;
do
logError(SysError("opening file '%1%'", nixCode.errPos.file).info());
else
{
line = readLine(fd.get());
++count;
if (count < pl)
// count the newlines.
int count = 0;
string line;
int pl = nixCode.errPos.line - 1;
do
{
;
}
else if (count == pl) {
nixCode.prevLineOfCode = line;
} else if (count == pl + 1) {
nixCode.errLineOfCode = line;
} else if (count == pl + 2) {
nixCode.nextLineOfCode = line;
break;
}
} while (true);
line = readLine(fd.get());
++count;
if (count < pl)
{
;
}
else if (count == pl) {
nixCode.prevLineOfCode = line;
} else if (count == pl + 1) {
nixCode.errLineOfCode = line;
} else if (count == pl + 2) {
nixCode.nextLineOfCode = line;
break;
}
} while (true);
}
}
catch (EndOfFile &eof) {
;
@ -103,7 +104,6 @@ void getCodeLines(NixCode &nixCode)
do
{
std::getline(iss, line);
// std::cout << "getline result: " << std::getline(iss, line) << std::endl;
++count;
if (count < pl)
{
@ -261,12 +261,12 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
break;
}
case foString: {
out << fmt("%1%from command line argument", prefix) << std::endl;
out << fmt("%1%from command line argument %2%", prefix, showErrPos(einfo.nixCode->errPos)) << std::endl;
out << prefix << std::endl;
break;
}
case foStdin: {
out << fmt("%1%from stdin", prefix) << std::endl;
out << fmt("%1%from stdin %2%", prefix, showErrPos(einfo.nixCode->errPos)) << std::endl;
out << prefix << std::endl;
break;
}
@ -291,11 +291,12 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
printCodeLines(out, prefix, nixcode);
out << prefix << std::endl;
}
}
// hint
out << prefix << *einfo.hint << std::endl;
out << prefix << std::endl;
// hint
if (einfo.hint.has_value()) {
out << prefix << *einfo.hint << std::endl;
out << prefix << std::endl;
}
return out;

View file

@ -16,6 +16,11 @@ nix-env --foo 2>&1 | grep "no operation"
nix-env -q --foo 2>&1 | grep "unknown flag"
# Eval Errors.
eval_res=$(nix-instantiate --eval -E 'let a = {} // a; in a.foo' 2>&1 || true)
echo $eval_res | grep "(string) (1:15)"
echo $eval_res | grep "infinite recursion encountered"
eval_arg_res=$(nix-instantiate --eval -E 'let a = {} // a; in a.foo' 2>&1 || true)
echo $eval_arg_res | grep "command line argument (1:15)"
echo $eval_arg_res | grep "infinite recursion encountered"
eval_stdin_res=$(echo 'let a = {} // a; in a.foo' | nix-instantiate --eval -E - 2>&1 || true)
echo $eval_stdin_res | grep "stdin (1:15)"
echo $eval_stdin_res | grep "infinite recursion encountered"