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 \ misc/upstart/local.mk \
doc/manual/local.mk \ doc/manual/local.mk \
tests/local.mk \ tests/local.mk \
tests/plugins/local.mk tests/plugins/local.mk \
src/error-demo/local.mk
-include Makefile.config -include Makefile.config

View file

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

View file

@ -197,7 +197,22 @@ std::ostream & operator << (std::ostream & str, const Pos & pos)
if (!pos) if (!pos)
str << "undefined position"; str << "undefined position";
else 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; return str;
} }

View file

@ -58,34 +58,35 @@ void getCodeLines(NixCode &nixCode)
if (nixCode.errPos.line <= 0) if (nixCode.errPos.line <= 0)
return; return;
// check this magic value!
if (nixCode.errPos.origin == foFile) { if (nixCode.errPos.origin == foFile) {
try { try {
AutoCloseFD fd = open(nixCode.errPos.file.c_str(), O_RDONLY | O_CLOEXEC); AutoCloseFD fd = open(nixCode.errPos.file.c_str(), O_RDONLY | O_CLOEXEC);
if (!fd) if (!fd)
throw SysError("opening file '%1%'", nixCode.errPos.file); logError(SysError("opening file '%1%'", nixCode.errPos.file).info());
else
// count the newlines.
int count = 0;
string line;
int pl = nixCode.errPos.line - 1;
do
{ {
line = readLine(fd.get()); // count the newlines.
++count; int count = 0;
if (count < pl) string line;
int pl = nixCode.errPos.line - 1;
do
{ {
; line = readLine(fd.get());
} ++count;
else if (count == pl) { if (count < pl)
nixCode.prevLineOfCode = line; {
} else if (count == pl + 1) { ;
nixCode.errLineOfCode = line; }
} else if (count == pl + 2) { else if (count == pl) {
nixCode.nextLineOfCode = line; nixCode.prevLineOfCode = line;
break; } else if (count == pl + 1) {
} nixCode.errLineOfCode = line;
} while (true); } else if (count == pl + 2) {
nixCode.nextLineOfCode = line;
break;
}
} while (true);
}
} }
catch (EndOfFile &eof) { catch (EndOfFile &eof) {
; ;
@ -103,7 +104,6 @@ void getCodeLines(NixCode &nixCode)
do do
{ {
std::getline(iss, line); std::getline(iss, line);
// std::cout << "getline result: " << std::getline(iss, line) << std::endl;
++count; ++count;
if (count < pl) if (count < pl)
{ {
@ -261,12 +261,12 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
break; break;
} }
case foString: { 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; out << prefix << std::endl;
break; break;
} }
case foStdin: { 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; out << prefix << std::endl;
break; break;
} }
@ -291,11 +291,12 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
printCodeLines(out, prefix, nixcode); printCodeLines(out, prefix, nixcode);
out << prefix << std::endl; out << prefix << std::endl;
} }
}
// hint // hint
out << prefix << *einfo.hint << std::endl; if (einfo.hint.has_value()) {
out << prefix << std::endl; out << prefix << *einfo.hint << std::endl;
out << prefix << std::endl;
} }
return out; 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" nix-env -q --foo 2>&1 | grep "unknown flag"
# Eval Errors. # Eval Errors.
eval_res=$(nix-instantiate --eval -E 'let a = {} // a; in a.foo' 2>&1 || true) eval_arg_res=$(nix-instantiate --eval -E 'let a = {} // a; in a.foo' 2>&1 || true)
echo $eval_res | grep "(string) (1:15)" echo $eval_arg_res | grep "command line argument (1:15)"
echo $eval_res | grep "infinite recursion encountered" 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"