toJSON: Add attribute path to trace

This commit is contained in:
Robert Hensing 2023-06-30 01:29:11 +02:00
parent 2d1d81114d
commit 33d58a90c2
4 changed files with 88 additions and 3 deletions

View file

@ -6,3 +6,5 @@
[`builtins.flakeRefToString`](@docroot@/language/builtins.md#builtins-flakeRefToString), [`builtins.flakeRefToString`](@docroot@/language/builtins.md#builtins-flakeRefToString),
have been added. have been added.
These functions are useful for converting between flake references encoded as attribute sets and URLs. These functions are useful for converting between flake references encoded as attribute sets and URLs.
- [`builtins.toJSON`](@docroot@/language/builtins.md#builtins-parseFlakeRef) now prints [--show-trace](@docroot@/command-ref/conf-file.html#conf-show-trace) items for the path in which it finds an evaluation error.

View file

@ -43,6 +43,7 @@ json printValueAsJSON(EvalState & state, bool strict,
break; break;
case nNull: case nNull:
// already initialized as null
break; break;
case nAttrs: { case nAttrs: {
@ -59,7 +60,13 @@ json printValueAsJSON(EvalState & state, bool strict,
names.emplace(state.symbols[j.name]); names.emplace(state.symbols[j.name]);
for (auto & j : names) { for (auto & j : names) {
Attr & a(*v.attrs->find(state.symbols.create(j))); Attr & a(*v.attrs->find(state.symbols.create(j)));
try {
out[j] = printValueAsJSON(state, strict, *a.value, a.pos, context, copyToStore); 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 } else
return printValueAsJSON(state, strict, *i->value, i->pos, context, copyToStore); return printValueAsJSON(state, strict, *i->value, i->pos, context, copyToStore);
@ -68,8 +75,17 @@ json printValueAsJSON(EvalState & state, bool strict,
case nList: { case nList: {
out = json::array(); out = json::array();
for (auto elem : v.listItems()) int i = 0;
for (auto elem : v.listItems()) {
try {
out.push_back(printValueAsJSON(state, strict, *elem, pos, context, copyToStore)); out.push_back(printValueAsJSON(state, strict, *elem, pos, context, copyToStore));
} catch (Error & e) {
e.addTrace({},
hintfmt("while evaluating list element at index %1%", i));
throw;
}
i++;
}
break; break;
} }

View file

@ -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 at index 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

View file

@ -0,0 +1,10 @@
builtins.toJSON {
a.b = [
true
false
"it's a bird"
{
c.d = throw "hah no";
}
];
}