# MathKernel doesn't return all Messages

In Mathematica I run this command:

Plot[Sin[x], {x, 1, 15}, GridLines -> error]


It generates a Plot with this error message:

A GridLines specification should be None, Automatic, or a list of grid line specifications.

When I run the same command via .Net Link it doesn't return the error message:

I've debugged the Math Kernel code and the MathKernelPacketHandler method in the MathKernel.cs class doesn't add the message.

The funny thing is if I run this command, it WILL return a message:

Plot[Sin[x], {x, 1, 15}, DateLabelFormat -> "aaa"]


Does anyone know if I can capture both failure messages?

Lastly I should point out that CaptureMessages is not used in the code. If you set it true or false it has no effect in the Kernel. Possibly a bug, but causes no problems.

• You're right about the non-use of CaptureMessages (and also CapturePrint). I just fixed that, so the values of these properties are respected, and the defaults changed to True, thus preserving the current behavior. This is why it's good to give out one's source code! – Todd Gayley Jul 24 '13 at 15:43
• I added the tags kernel and mathlink as this is a more general problem than .netlink. – rcollyer Jul 24 '13 at 15:52
• I just wanted to acknowledge all the help everyone has given on this topic. Thanks very much. I've upvoted all answers, just need a bit of time to either get this working or decide if I'll put this aside and leave it as a FrontEnd feature. – WolframFan Jul 25 '13 at 6:37

To access the errors, you need to invoke the Front End directly from the kernel. In effect, you end up telling the kernel to tell the FE to tell the kernel to do something, so that the FE can report any errors it finds. The method I use is

ClearAll[getFrontEndErrors];
SetAttributes[getFrontEndErrors, HoldAllComplete];

getFrontEndErrors[expr_] :=
Block[{nb, pinks},
UsingFrontEnd[
nb = CreateDocument[ExpressionCell[expr, "Output"],
Visible -> False, NotebookFileName -> "FEMessages"];
SelectionMove[nb, All, Cell];
pinks = MathLinkCallFrontEnd[FrontEndGetErrorsInSelectionPacket[nb]];
NotebookClose[nb]
];
pinks
];


which only returns the FE errors. (Edit: I removed the use of InternalWithLocalSettings as it has limitations that are counterproductive here.) Note, this creates and destroys a notebook. But, if you are posting the info to an existing notebook, you can bypass those steps and just call

MathLinkCallFrontEnd[FrontEndGetErrorsInSelectionPacket[nb]]


which returns an empty list if there are no errors present. (You may have to preced the above code with UsingFrontEnd.)

• Nice. I wonder how people learn about hidden things like FrontEndGetErrorsInSelectionPacket. Your function didn't work for me, though, until I took out the Visible->False. It seems that because the error isn't generated until rendering time, it never occurs if the notebook is not made visible. It's not clear how to integrate this into the workflow of the original question. There is no direct indication that there is a problem with the original graphic, so you would have to perform this step as an extra test on every single image. – Todd Gayley Jul 24 '13 at 16:27
• @JacobAkkerboom I've never been sure when to add Visible -> False or not. I do know if they are visible, you will see them blink in and out of existence. – rcollyer Jul 24 '13 at 17:09
• @ToddGayley I cheated. I learned it from someone else because I needed it for my testing, and yes you have to run it on every single image. I'm not sure how to integrate it into the OP's workflow, either, but it is a place to start. – rcollyer Jul 24 '13 at 17:11
• @JacobAkkerboom I've added a potential fix for the Visible -> False problem. If you would try it again, I would appreciate it. – rcollyer Oct 9 '13 at 12:57
• @rcollyer I think it works fine now :). – Jacob Akkerboom Oct 9 '13 at 13:44

The problem here is that the Gridlines specification error message is not a kernel error message (you'll note that it is not printed with the standard Func::tag format). Instead, this warning text is generated by the front end during the rendering of the graphic. The actual generation of the gridlines values is deferred to the moment when the graphics expression created by the kernel is actually rendered into an image by the front end. From the perspective of the kernel, there is nothing wrong with your erroneous GridLines specification.

The front end does not report these errors in any way I know of, so I'm afraid I see no reasonable way of getting this information in any external program that calls Mathematica for graphics, whether it is .NET, Java, C, etc. The image you get back will have a pink error background, but I don't think there is a way to get information about the specific cause of the error.

• There is a method, see my answer. – rcollyer Jul 24 '13 at 15:51