Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Print a message and return a failure object
| ResourceFunction["MessageFailure"]["tag","message"] uses "tag" as the Failure tag. | |
| ResourceFunction["MessageFailure"]["tag",<|…|>] determines the message text and failure details from the given Association. | |
| ResourceFunction["MessageFailure"][symbol::tag] prints the message symbol::tag and returns a corresponding Failure object. | |
| ResourceFunction["MessageFailure"][symbol::tag,e1,e2] prints a message and returns a Failure, inserting the values of the ei as needed. | 
| "MessageTemplate" | a string template for a message | 
| "MessageParameters" | parameters to use for the message template | 
Print a generic error message and return a Failure object:
| In[1]:= | ![ResourceFunction["MessageFailure"][]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/21ca40fcabb6bb5b.png) | 
| Out[1]= |  | 
Specify the message text:
| In[2]:= | ![ResourceFunction["MessageFailure"]["This is an error message."]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/231657efb3d381c6.png) | 
| Out[2]= |  | 
Specify a tag:
| In[3]:= | ![ResourceFunction["MessageFailure"]["MyTag", "This is an error message."]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/7f872c876b47946d.png) | 
| Out[3]= |  | 
Choose a generic message based on the given tag:
| In[4]:= | ![ResourceFunction["MessageFailure"]["MyTag", Automatic]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/10af388c7867f219.png) | 
| Out[4]= |  | 
Use Message syntax:
| In[5]:= | ![ResourceFunction["MessageFailure"][f::argx, f, 2]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/2dcabc9645c3b25d.png) | 
| Out[5]= |  | 
| In[6]:= | ![ResourceFunction["MessageFailure"][Power::infy, HoldForm[1/0]]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/71b52befe06d5acb.png) | 
| Out[6]= |  | 
Use Failure syntax:
| In[7]:= | ![ResourceFunction[
 "MessageFailure"]["InvalidRange", <|
  "MessageTemplate" -> "Enter a number lower than `Number`", "MessageParameters" -> <|"Number" -> 10|>|>]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/0da5d0b19f6bbeae.png) | 
| Out[7]= |  | 
| In[8]:= | ![ResourceFunction[
 "MessageFailure"]["InvalidInput", <|
  "Message" -> "Input must be a string"|>]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/57dda197ff0f9dfd.png) | 
| Out[8]= |  | 
Include additional metadata:
| In[9]:= | ![ResourceFunction["MessageFailure"]["RestrictionFailure",
 <|"MessageTemplate" :> Interpreter::numberinterval,
  "MessageParameters" -> <|"Min" -> 1, "Max" -> 10, "Input" -> {"100"}|>,
  "Interval" -> Interval[{1, 10}],
  "Input" -> {"100"},
  "Type" -> "Number"|>
 ]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/6a3de4dcbf9b65f6.png) | 
| Out[9]= |  | 
Specify a different message symbol:
| In[10]:= | ![ResourceFunction["MessageFailure"]["RestrictionFailure",
 <|"MessageTemplate" :> Interpreter::numberinterval,
  "MessageParameters" -> <|"Min" -> 1, "Max" -> 10, "Input" -> {"100"}|>,
  "MessageSymbol" -> MyFunction|>
 ]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/704cc7e0c15a91b8.png) | 
| Out[10]= |  | 
Use a custom message function:
| In[11]:= | ![ResourceFunction["MessageFailure"][Power::infy, HoldForm[1/0], "MessageFunction" -> Print@*List]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/3b3a789a5604491c.png) | 

| Out[11]= |  | 
Issue the message with ResourceFunctionMessage instead of Message:
| In[12]:= | ![ResourceFunction["MessageFailure"][Power::infy, HoldForm[1/0], "MessageFunction" -> ResourceFunction["ResourceFunctionMessage"]]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/1d82104c13be19c2.png) | 
| Out[12]= |  | 
Define a function that uses the default setting "MessageFunction"→Automatic:
| In[13]:= | ![AddOne // ClearAll;
AddOne::integer = "`1` is expected to be an integer.";
AddOne[x_Integer] := x + 1;
AddOne[other_] := ResourceFunction["MessageFailure"][AddOne::integer, other];](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/78dddded0e577ca5.png) | 
By default, Message will be used:
| In[14]:= |  | 
| Out[14]= |  | 
Once converted to a resource function, ResourceFunctionMessage will be automatically used instead:
| In[15]:= | ![rfAddOne = DefineResourceFunction[AddOne]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/23dba48e1712327e.png) | 
| Out[15]= |  | 
| In[16]:= |  | 
| Out[16]= |  | 
Use SetOptions to change this globally:
| In[17]:= | ![SetOptions[ResourceFunction["MessageFailure"], "MessageFunction" -> Message]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/4f100368bf411137.png) | 
| Out[17]= |  | 
| In[18]:= |  | 
| Out[18]= |  | 
Restore the default setting:
| In[19]:= | ![SetOptions[ResourceFunction["MessageFailure"], "MessageFunction" -> Automatic];](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/7de81fca486516d3.png) | 
By default, only a small number of keys are included in the Failure object:
| In[20]:= | ![simple = ResourceFunction["MessageFailure"][Power::infy, HoldForm[1/0]]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/6bda507b33c0f74d.png) | 
| Out[20]= |  | 
| In[21]:= | ![simple[[2]]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/45ab9420bb7e0070.png) | 
| Out[21]= |  | 
Use "Verbose"→True to include additional metadata used internally by MessageFailure:
| In[22]:= | ![verbose = ResourceFunction["MessageFailure"][Power::infy, HoldForm[1/0], "Verbose" -> True]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/3ec8d042d969bded.png) | 
| Out[22]= |  | 
| In[23]:= | ![verbose[[2]]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/033e020a1baedfeb.png) | 
| Out[23]= |  | 
Include a stack trace in the failure metadata:
| In[24]:= | ![rsqrt[x_] := If[TrueQ[x >= 0], Sqrt[x], ResourceFunction["MessageFailure"][rsqrt::nnarg, x, "Stack" -> True]];
rsqrt::nnarg = "The argument `1` is not greater than or equal to zero.";](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/574e6d18bacc56b0.png) | 
| In[25]:= | ![f[g[1, fail = rsqrt[-2.25]; 2]]; fail](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/58063c3dd5c83468.png) | 
| Out[25]= |  | 
View the stack trace:
| In[26]:= | ![fail["Stack"] // Column](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/54747d45d3409168.png) | 
| Out[26]= |  | 
Include a more detailed stack trace in the failure metadata:
| In[27]:= | ![rsqrt[x_] := If[TrueQ[x >= 0], Sqrt[x], ResourceFunction["MessageFailure"][rsqrt::nnarg, x, "Stack" -> Full]];
rsqrt::nnarg = "The argument `1` is not greater than or equal to zero.";](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/4652ec8d852eed80.png) | 
| In[28]:= | ![f[g[1, fail = rsqrt[-2.25]; 2]]; fail](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/198898f196e95c9d.png) | 
| Out[28]= |  | 
View the stack trace:
| In[29]:= | ![fail["Stack"] // Column](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/5317e4379e8960af.png) | 
| Out[29]= |  | 
Use StackComplete for even more detail:
| In[30]:= | ![StackComplete[f[g[1, fail = rsqrt[-2.25]; 2]]; fail]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/6ed27ea32a5e1017.png) | 
| Out[30]= |  | 
| In[31]:= | ![fail["Stack"] // Column](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/771a498d12d79971.png) | 
| Out[31]= |  | 
When using the option setting "Verbose"→True, the stack will also include evaluations made by MessageFailure as well:
| In[32]:= | ![rsqrt[x_] := If[TrueQ[x >= 0], Sqrt[x], ResourceFunction["MessageFailure"][rsqrt::nnarg, x, "Stack" -> Full, "Verbose" -> True]];
rsqrt::nnarg = "The argument `1` is not greater than or equal to zero.";](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/701302000e41710b.png) | 
| In[33]:= | ![f[g[1, fail = rsqrt[-2.25]; 2]]; fail](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/532bd5ee70ed1124.png) | 
| Out[33]= |  | 
The stack trace now includes internal evaluations of MessageFailure as well:
| In[34]:= | ![fail["Stack"] // Map[Short] // Column](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/038c916b9fe3666c.png) | 
| Out[34]= |  | 
Create directives for defining new functions:
| In[35]:= | ![BeginDefinition // ClearAll;
BeginDefinition // Attributes = {HoldFirst};
BeginDefinition[s_Symbol] := (Unprotect[s]; ClearAll[s]);](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/222e3fb20dfcde6d.png) | 
| In[36]:= | ![EndDefinition // ClearAll;
EndDefinition // Attributes = {HoldFirst};
EndDefinition[s_Symbol] := (
   expr : s[___] := ResourceFunction["MessageFailure"][
     s, <|"MessageTemplate" -> "No definition found for `1`.", "MessageParameters" -> {HoldForm@expr}|>];
   Protect@s
   );](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/3595b423cdc346cd.png) | 
Define a new function using these directives:
| In[37]:= | ![MyFunction // BeginDefinition;
MyFunction[x_Integer] := x + 1;
MyFunction[x_Integer, y_Integer] := x + y;
MyFunction // EndDefinition;](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/6f0ff51ab2be8e8f.png) | 
Test the function:
| In[38]:= | ![MyFunction[5]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/1c7721ab5734531f.png) | 
| Out[38]= |  | 
| In[39]:= | ![MyFunction[5, 6]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/5b106362750d70d2.png) | 
| Out[39]= |  | 
Undefined inputs are automatically caught as an error:
| In[40]:= | ![MyFunction[a, b, c]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/23c6702ad3949bb0.png) | 
| Out[40]= |  | 
The message can be retrieved from the Failure object:
| In[41]:= | ![failure = ResourceFunction["MessageFailure"][Power::infy, HoldForm[1/0]]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/19debe9098deeb93.png) | 
| Out[41]= |  | 
| In[42]:= | ![failure["Message"]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/35e2e4b0e2760e4c.png) | 
| Out[42]= |  | 
A message can be reissued from the Failure object using Message:
| In[43]:= |  | 
| In[44]:= | ![failure = ResourceFunction["MessageFailure"][func::number, abc]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/5380ea71eb068b86.png) | 
| Out[44]= |  | 
| In[45]:= | ![Message[Evaluate[failure]]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/026c7bdf3c4dc802.png) | 
MessageFailure can also reissue the message from the Failure:
| In[46]:= | ![ResourceFunction["MessageFailure"][failure]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/1cc65ef0749cd5ec.png) | 
| Out[46]= |  | 
MessageFailure can use modern template specifications:
| In[47]:= | ![MyFunction::datedmsg = "<*DateString[]*>: `ErrorMessage`";](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/7b9dab0d96fd6c8d.png) | 
| In[48]:= | ![ResourceFunction["MessageFailure"][
 MyFunction::datedmsg, <|"ErrorMessage" -> "Something broke!"|>]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/4c0da85c956b66f5.png) | 
| Out[48]= |  | 
Compare to Message:
| In[49]:= | ![Message[MyFunction::datedmsg, <|"ErrorMessage" -> "Something broke!"|>]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/1a7495cc8f88aa75.png) | 
Message templates need not be strings:
| In[50]:= | ![ResourceFunction["MessageFailure"]["MyTag",
  <|"MessageTemplate" :> Graphics[{Circle[], Inset[TemplateSlot["ErrorMessage"], {0, 0}]}],
    "MessageParameters" -> <|"ErrorMessage" -> "Something broke!"|>|>];](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/1a47b12d53488961.png) | 
MessageFailure can print messages for Failure objects that use named template parameters:
| In[51]:= | ![failure = Failure["InvalidRange", <|
   "MessageTemplate" -> "Enter a number lower than `Number`",
   "MessageParameters" -> <|"Number" -> 10|>
   |>]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/4eaff0bee5dfe6f1.png) | 
| Out[51]= |  | 
| In[52]:= | ![ResourceFunction["MessageFailure"][failure]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/432b6d1b3e337e8b.png) | 
| Out[52]= |  | 
Compare to Message:
| In[53]:= | ![Message[Evaluate[failure]]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/7d742c338a331190.png) | 
| Out[53]= |  | 
Messages with named template slots are temporarily redefined to avoid giving unsupported templates to Message:
| In[54]:= |  | 
| In[55]:= | ![ResourceFunction["MessageFailure"][
 MySymbol::testing, <|"NamedParameter" -> 123|>]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/387d270659e8eaad.png) | 
| Out[55]= |  | 
Use a message handler to print the current message definition at the time of message generation:
| In[56]:= | ![Internal`HandlerBlock[{"Message", If[#[[2]], Extract[#, {1, 1}, EchoEvaluation]] &},
 ResourceFunction["MessageFailure"][
  MySymbol::testing, <|"NamedParameter" -> 123|>]
 ]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/01da1c10394be3b3.png) | 
| Out[56]= |  | 
The original message definition is restored:
| In[57]:= |  | 
| Out[57]= |  | 
Using named template slots or template expressions in message definitions gives a template that is not supported by Message:
| In[58]:= | ![MyFunction::datedmsg = "<*DateString[]*>: `ErrorMessage`";](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/17243e285ac5ca0a.png) | 
| In[59]:= | ![Message[MyFunction::datedmsg, <|"ErrorMessage" -> "Something broke!"|>]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/1e63a25056c3d63a.png) | 
Use MessageFailure to issue these kinds of messages instead:
| In[60]:= | ![ResourceFunction["MessageFailure"][
 MyFunction::datedmsg, <|"ErrorMessage" -> "Something broke!"|>]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/4db15a7084f525b3.png) | 
| Out[60]= |  | 
Non-string templates won't appear formatted in the resulting Failure object:
| In[61]:= | ![failure = ResourceFunction["MessageFailure"][
  "AThingBroke",
  <|
   "MessageTemplate" :> Row[{DateString[], " ", TemplateSlot["ErrorMessage"]}],
   "MessageParameters" -> <|"ErrorMessage" -> "Something broke."|>
   |>
  ]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/3f357aa0cc302642.png) | 
| Out[61]= |  | 
The printed message cannot be retrieved from the failure either:
| In[62]:= | ![failure["Message"]](https://www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/581435d6c3a46428.png) | 
| Out[62]= |  | 
Wolfram Language 14.0 (January 2024) or above
This work is licensed under a Creative Commons Attribution 4.0 International License