[Nix-dev] Re: Proposal: tryCatch function

Nicolas Pierron nicolas.b.pierron at gmail.com
Mon Jan 12 19:58:04 CET 2009


Hi list,

Ok, here is the final suggested patch to add messages inside the stack trace:

so what you get in your code is:

foo = addLocation "while evaluating /something more explicit than foo/:" value;

and as the opposite of trace statements you only get them if there is
a failure, so you don't have to remove them from your code.

On Sat, Jan 3, 2009 at 11:17, Nicolas Pierron
<nicolas.b.pierron at gmail.com> wrote:
> Hi,
>
> Many people have reported that message are not very helpful and this
> is really the case inside NixOS where everything is merged and you are
> not able to figure out what is the problem.  So I have made a patch
> for nix which add a builtin called "tryCatch".  The tryCatch function
> expect a value and a function which expect one string argument.
>
> The following test file is used to show how this function works.  It
> contains a tryCatch function build over the builtin (should be added
> to pkgs.lib.).
>
> .----------------- exception.nix
> | let
> |   tryCatch =
> |     if builtins ? tryCatch then
> |       builtins.tryCatch
> |     else
> |       builtins.trace "tryCatch builtin not found. Update your nix version."
> |       (try: catch: try);
> |
> |   v1 = builtins.throw "v1: throw message";
> |   v2 = tryCatch v1 (msg: builtins.throw "v2: ${msg}");
> |   v3 = tryCatch v2 (msg: builtins.throw "v3: ${msg}");
> |   v4 = tryCatch v3 (msg: builtins.throw "v4: ${msg}");
> |   v5 = tryCatch v4 (msg: builtins.throw "v5: ${msg}");
> |   v6 = tryCatch v5 (msg: builtins.throw "v6: ${msg}");
> |   v7 = tryCatch v6 (msg: builtins.throw "v7: ${msg}");
> |   v8 = tryCatch v7 (msg: builtins.throw "v8: ${msg}");
> |   v9 = tryCatch v8 (msg: builtins.throw "v9: ${msg}");
> |   v10 = tryCatch v9 (msg: builtins.throw "v10: ${msg}");
> | in v10
> `-----------------
>
> If your are using a nix version which does not support this builtin
> you will get the following message:
>
> trace: Str("tryCatch builtin not found. Update your nix version.",[])
> error: while evaluating the attribute `<let-body>' at
> `/tmp/test-nix/exception.nix', line 1:
> while evaluating the attribute `v10' at `/tmp/test-nix/exception.nix', line 18:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 7:
> while evaluating the attribute `v9' at `/tmp/test-nix/exception.nix', line 17:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 7:
> while evaluating the attribute `v8' at `/tmp/test-nix/exception.nix', line 16:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 7:
> while evaluating the attribute `v7' at `/tmp/test-nix/exception.nix', line 15:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 7:
> while evaluating the attribute `v6' at `/tmp/test-nix/exception.nix', line 14:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 7:
> while evaluating the attribute `v5' at `/tmp/test-nix/exception.nix', line 13:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 7:
> while evaluating the attribute `v4' at `/tmp/test-nix/exception.nix', line 12:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 7:
> while evaluating the attribute `v3' at `/tmp/test-nix/exception.nix', line 11:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 7:
> while evaluating the attribute `v2' at `/tmp/test-nix/exception.nix', line 10:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 7:
> while evaluating the attribute `v1' at `/tmp/test-nix/exception.nix', line 9:
> user-thrown exception: `v1: throw message'
>
> If you are using a recent version of nix which handle this function
> you will get:
>
> error: while evaluating the attribute `<let-body>' at
> `/tmp/test-nix/exception.nix', line 1:
> while evaluating the attribute `v10' at `/tmp/test-nix/exception.nix', line 18:
> while evaluating the function at `/tmp/test-nix/exception.nix', line 18:
> user-thrown exception: `v10: v9: v8: v7: v6: v5: v4: v3: v2: v1: throw message'
>
> In this particular case we should get better messages with much more
> context information.
> By the way you have to take care of function, for example if v8 is a
> function, then v10 and v9 won't appear in the message.
>
>
> --
> Nicolas Pierron
> http://www.linkedin.com/in/nicolasbpierron
> - If you are doing something twice then you should try to do it once.
>



-- 
Nicolas Pierron
http://www.linkedin.com/in/nicolasbpierron
- If you are doing something twice then you should try to do it once.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: addLocation.patch
Type: application/octet-stream
Size: 1007 bytes
Desc: not available
Url : http://lists.science.uu.nl/pipermail/nix-dev/attachments/20090112/969b41a8/attachment.obj 


More information about the nix-dev mailing list