Skip to content

Commit

Permalink
toJSON: Add attribute path to trace
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth committed Jul 31, 2023
1 parent 2d1d811 commit f3987b3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/libexpr/value-to-json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ json printValueAsJSON(EvalState & state, bool strict,
break;

case nNull:
// already initialized as null
break;

case nAttrs: {
Expand All @@ -59,7 +60,13 @@ json printValueAsJSON(EvalState & state, bool strict,
names.emplace(state.symbols[j.name]);
for (auto & j : names) {
Attr & a(*v.attrs->find(state.symbols.create(j)));
out[j] = printValueAsJSON(state, strict, *a.value, a.pos, context, copyToStore);
try {
out[j] = printValueAsJSON(state, strict, *a.value, a.pos, context, copyToStore);
} catch (Error & e) {
e.addTrace(state.positions[a.pos],
hintfmt("while evaluating attribute '%1%'", j));
throw;
}
}
} else
return printValueAsJSON(state, strict, *i->value, i->pos, context, copyToStore);
Expand All @@ -68,8 +75,17 @@ json printValueAsJSON(EvalState & state, bool strict,

case nList: {
out = json::array();
for (auto elem : v.listItems())
out.push_back(printValueAsJSON(state, strict, *elem, pos, context, copyToStore));
int i = 0;
for (auto elem : v.listItems()) {
try {
out.push_back(printValueAsJSON(state, strict, *elem, pos, context, copyToStore));
} catch (Error & e) {
e.addTrace({},
hintfmt("while evaluating list element %1%", i));
throw;
}
i++;
}
break;
}

Expand Down
57 changes: 57 additions & 0 deletions tests/lang/eval-fail-toJSON.err.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
error:
… while calling the 'toJSON' builtin

at /pwd/lang/eval-fail-toJSON.nix:1:1:

1| builtins.toJSON {
| ^
2| a.b = [

… while evaluating attribute 'a'

at /pwd/lang/eval-fail-toJSON.nix:2:3:

1| builtins.toJSON {
2| a.b = [
| ^
3| true

… while evaluating attribute 'b'

at /pwd/lang/eval-fail-toJSON.nix:2:3:

1| builtins.toJSON {
2| a.b = [
| ^
3| true

… while evaluating list element 3

… while evaluating attribute 'c'

at /pwd/lang/eval-fail-toJSON.nix:7:7:

6| {
7| c.d = throw "hah no";
| ^
8| }

… while evaluating attribute 'd'

at /pwd/lang/eval-fail-toJSON.nix:7:7:

6| {
7| c.d = throw "hah no";
| ^
8| }

… while calling the 'throw' builtin

at /pwd/lang/eval-fail-toJSON.nix:7:13:

6| {
7| c.d = throw "hah no";
| ^
8| }

error: hah no
10 changes: 10 additions & 0 deletions tests/lang/eval-fail-toJSON.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
builtins.toJSON {
a.b = [
true
false
"it's a bird"
{
c.d = throw "hah no";
}
];
}

0 comments on commit f3987b3

Please sign in to comment.