Function Repository Resource:

AddAutomaticConfirmInfo

Source Notebook

Tag Confirm-type errors with the code that generated the error

Contributed by: Sjoerd Smit

ResourceFunction["AddAutomaticConfirmInfo"][symbol]

modifies the definition of symbol to add error information to all Confirm-type operations.

ResourceFunction["AddAutomaticConfirmInfo"]["symbol"]

works with the symbol named "symbol" if it exists.

Details

ResourceFunction["AddAutomaticConfirmInfo"] is a meta-programming utility primarily aimed at developers of paclets.
Enclose and Confirm are useful error-catching constructs but when they are used in a heavily nested fashion, it can be difficult to track down where in the source code an error originates from. ResourceFunction["AddAutomaticConfirmInfo"] tries to address this by decorating each Confirm-type statement with a string representing the InputForm of the original code (without context information for readability) to make it easier to find the line of code that caused the error.
ResourceFunction["AddAutomaticConfirmInfo"] returns the modified Definition of symbol.

Examples

Basic Examples

Define a function that uses ConfirmBy to validate steps in the computation:

In[1]:=
fun1[x_] := Module[{y = x}, Enclose[ConfirmBy[y^2, OddQ]]];

The error generated does not mention the symbol y in the original function definition:

In[2]:=
fun1[4]
Out[2]=

Use AddAutomaticConfirmInfo to populate the third argument of ConfirmBy in the function definition:

In[3]:=
ResourceFunction["AddAutomaticConfirmInfo"][fun1]
Out[3]=

Now the Failure includes useful information:

In[4]:=
fun1[4]
Out[4]=

Click the Association in the expanded error information to copy the full symbol name and the InputForm of the original code that caused the error:

Define a second function that uses the first one:

In[5]:=
fun2[x_] := Module[{y},
   Enclose[
    y = ConfirmBy[Sqrt[x], IntegerQ];
    Confirm @fun1[x + y]
    ]
   ];
ResourceFunction["AddAutomaticConfirmInfo"][fun2];

An error generated by the first ConfirmBy:

In[6]:=
fun2[5]
Out[6]=

An error propagated up from the first function:

In[7]:=
fun2[4]
Out[7]=

Use ResourceFunction["FlattenFailure"] to make nested errors easier to inspect:

In[8]:=
% // ResourceFunction["FlattenFailure"]
Out[8]=

Extract the deepest error from the Failure object:

In[9]:=
%["Information"] // Last
Out[9]=

Publisher

Sjoerd Smit

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 21 February 2024

Related Resources

License Information