[Nix-dev] Proposal: tryCatch function

Nicolas Pierron nicolas.b.pierron at gmail.com
Tue Jan 6 12:49:53 CET 2009


On Tue, Jan 6, 2009 at 12:07, Eelco Dolstra <e.dolstra at tudelft.nl> wrote:
> Hi,
>
> Nicolas Pierron wrote:
>
>> 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.
>
> Can you give some example use cases of where this would help?  "throw" already
> takes a string argument, so the error message can be as helpful as you want it
> to be.  If the "throw" error message cannot show a helpful message because it
> lacks context (e.g. deep in some function call), the necessary context could be
> passed in as a string argument, for instance.

Ok, in merge function used for options there is an extra argument.
This extra argument is the name of the attribute which is computed by
the merge function.  The name is used to report the location inside
error messages.  If the input of an attribute is incorrect, you would
like to know which attribute cause the failure. (functions:
mergeDefaultOption, mergeTypedOption, .. user defined merge functions
..)  The name of the attribute is only used for reporting the location
of the error, so whatever you want to do you have to accept an extra
argument which gives you the location of the error.  This extra
argument is not correct IMHO because it add more complexity to a
function only to handle something which is not related to the goal of
the function.  The goal of a merge function is to merge (so one list
argument) and not to handle errors.

mergeSomething = list:
  try
    doTheMerging list
  catch
    throw "cannot merge.";

Passing the string argument just add more complexity for nothing.

Another solution which can be used to target this specific problem
could be to add an extra function which only replace the error prefix
of the throw/abort messages.

builtins.errorPrefix "${builtins.getErrorPrefix}.${attrName}" value

-- 
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