[Nix-dev] Usefulness of adderrorContext
Marc Weber
marco-oweber at gmx.de
Fri Nov 20 15:32:37 CET 2009
Run this example and you'll get:
== result
/tmp/test-nix-add-error-context.nix|1 col 4| error: while evaluating the attribute `<let-body>'
/tmp/test-nix-add-error-context.nix|8 col 8| while evaluating the function
/tmp/test-nix-add-error-context.nix|10 col 7| while evaluating the attribute `take'
/tmp/test-nix-add-error-context.nix|5 col 9| while evaluating the attribute `name'
|| value
|| value is an integer while an attribute set was expected
== code
let
inherit (builtins) addErrorContext;
attr = addErrorContext "attr" {
name = addErrorContext "value" (2).abc;
};
f = x: x;
take = attr.name;
in f take
So what happened to the error context "attr" ?
You can get both contexts by using addErrorContext2 instead of addErrorContext
addErrorContext2 = msg: a: addErrorContext msg (strict a);
However this isn't perfect because infinite recursions will be
evaluated etc.
strict could be implemented like this:
# evaluate everything once so that errors will occur earlier
# hacky: traverse attrs by adding a dummy
# ignores functions (should this behavior change?) See strictf
strict = x :
let
traverse = x :
if isString x then true
else if isAttrs x then
if x ? outPath then true
else all id (mapRecordFlatten (n: traverse) x)
else if isList x then
all id (map traverse x)
else if isBool x then true
else if isFunction x then true
else if isInt x then true
else if x == null then true
else true; # a (store) path?
in if (traverse x) then x else throw "else never reached";
So is this a bug or a feature that "attr" is lost in the first example?
Marc Weber
More information about the nix-dev
mailing list