Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a way to find an existing error message in the System` context, that is used for a certain case? I want to include a message in a custom function that compares all the arguments of the function, and checks if they have the same length. I'm pretty sure that there is one such, but I can't seem to find it. And I don't want to insert new symbols or messages to the namespace if I can use existing ones. Now I am looking for a method that is general; i.e., it can be applied to other cases, not just this one with argument lengths.

For example I am looking for a message that works like this:

General::badlen = "The length of arguments `1`, `2`, `3` must be the same.";

myFun[x_List, y_List, z_List] /; 
  If[Length@x == Length@y == Length@z, True,
     Message[myFun::badlen, x, y, z]; False] := x*y*z;
share|improve this question
1  
The nearest message that I am aware of is General::bldim... anyway, you might want to look for appropriate messages that you can use within Messages.m, which is in the directory $InstallationDirectory <> "/SystemFiles/Kernel/TextResources/English". –  J. M. Nov 17 '12 at 11:46
    
Thanks @J.M. It could be a perfect answer... –  István Zachar Nov 17 '12 at 12:38
    
I don't think it's a completely optimal message, tho. You'll of course need to inscribe additional code to determine which two of the three arguments given have the incompatibility... also, there might be a better error message within Messages.m; I just got lazy after five minutes. –  J. M. Nov 17 '12 at 12:43
    
@J.M. Well, my Q is not about this specific error message (it could have been any other task), but the way how to access/query/find built-in error messages. –  István Zachar Nov 17 '12 at 12:58
add comment

2 Answers 2

up vote 9 down vote accepted

Here's a tiny utility function you might use if you're trying to look for a message that contains a known string:

searchMessages[str_String, opts___] :=
Sort[Select[
     Flatten[Map[ToExpression[#, InputForm, Defer] :> Evaluate[ToExpression[#]] &,
                 StringCases[FindList[$InstallationDirectory <> 
                                      "/SystemFiles/Kernel/TextResources/English/Messages.m",
                                      str, opts, IgnoreCase -> True], 
                             RegularExpression["(\\w|`)+::(\\w)+"]], {2}]],
            With[{s = Last[#]}, StringQ[s] && ! StringFreeQ[s, str, IgnoreCase -> True]] &]]

For example,

searchMessages["discretization"]
   {NDSolve::tponly :> "Currently the only discretization method implemented is
    SpatialDiscretization -> TensorProductGrid."}

The function tries to search for apropos messages within the Messages.m file within the system files. It won't always find an appropriate message, since there are messages like General::offline that aren't within Messages.m (though grep tells me that that particular message is in $InstallationDirectory <> "SystemFiles/Autoload/PacletManager/Kernel/Services.m", so you know what to modify). Still, it's a start...

share|improve this answer
    
Thanks for the extra effort, I really appreciate it! –  István Zachar Nov 17 '12 at 14:21
    
No problem: as I said, I'm lazy, so I wrote a routine to do the searching for me... –  J. M. Nov 17 '12 at 15:24
1  
That's not the meaning of lazy. Lazy is doing this by hand repeatedly until someone else wrote searchMessages for me... :^) –  Mr.Wizard Nov 17 '12 at 16:34
    
@Mr.Wizard ... or for me. Now I feel the shame... :) –  István Zachar Nov 18 '12 at 14:36
add comment

We can scan the messages that have been loaded into the current session like this:

messages[pattern_, context_:"*"] :=
  Cases[
    Quiet[ToExpression[#, InputForm, Messages]& /@ Names[context~~"*"], General::readp]
  , (_[n_] :> m_String) /; !StringFreeQ[m, pattern, IgnoreCase -> True] :> HoldForm@n :> m
  , {2}
  ]

messages["arguments", "System`"]
(*
{General::bldim:>The arguments `1` and `2` in `3` do not have compatible dimensions.}
*)

messages["longer"]
(*
{General::partd:>Part specification `1` is longer than depth of object.,
 Part::partd:>Part specification `1` is longer than depth of object.}
*)

The emphasis here is on loaded messages. This method will not find any messages in packages that have not yet been loaded. Also beware that some packages load symbols (and their messages) lazily, on demand.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.