aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/error.cc
diff options
context:
space:
mode:
authorBen Burdette <bburdette@gmail.com>2020-05-20 17:25:02 -0600
committerBen Burdette <bburdette@gmail.com>2020-05-20 17:25:02 -0600
commit85ce455b854761b1fd4985ed21ef5a8881eb3c11 (patch)
tree455cd87bdcb6cf2cec199a033d43ea8b82375268 /src/libutil/error.cc
parent92123c6c798682f3a7c1f19984dcd3a06ad6be92 (diff)
get code lines from the nix file
Diffstat (limited to 'src/libutil/error.cc')
-rw-r--r--src/libutil/error.cc69
1 files changed, 60 insertions, 9 deletions
diff --git a/src/libutil/error.cc b/src/libutil/error.cc
index 59628d875..7aff9381b 100644
--- a/src/libutil/error.cc
+++ b/src/libutil/error.cc
@@ -53,6 +53,52 @@ string showErrPos(const ErrPos &errPos)
}
}
+void getCodeLines(NixCode &nixCode)
+{
+ if (nixCode.errPos.line <= 0)
+ return;
+
+ // check this magic value!
+ if (nixCode.errPos.file == "(string)")
+ return;
+
+ 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
+ {
+ 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) {
+ ;
+ }
+ catch (std::exception &e) {
+ printError("error reading nix file: %s\n%s", nixCode.errPos.file, e.what());
+ }
+}
+
+
void printCodeLines(std::ostream &out, const string &prefix, const NixCode &nixCode)
{
// previous line of code.
@@ -197,16 +243,21 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
out << prefix << std::endl;
}
- // lines of code.
- if (einfo.nixCode.has_value() && einfo.nixCode->errLineOfCode.has_value()) {
- printCodeLines(out, prefix, *einfo.nixCode);
- out << prefix << std::endl;
- }
- // hint
- if (einfo.hint.has_value()) {
- out << prefix << *einfo.hint << std::endl;
- out << prefix << std::endl;
+ if (einfo.nixCode.has_value()) {
+ NixCode nixcode = *einfo.nixCode;
+ getCodeLines(nixcode);
+
+ // lines of code.
+ if (nixcode.errLineOfCode.has_value()) {
+ printCodeLines(out, prefix, nixcode);
+ out << prefix << std::endl;
+ }
+
+ // hint
+ out << prefix << *einfo.hint << std::endl;
+ out << prefix << std::endl;
+
}
return out;