[Nix-dev] Proposal: tryCatch function

Nicolas Pierron nicolas.b.pierron at gmail.com
Sat Jan 3 11:17:19 CET 2009


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.



More information about the nix-dev mailing list