22
$\begingroup$

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:

enter image description here

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.

$\endgroup$
3
  • 2
    $\begingroup$ 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! $\endgroup$ Commented Jul 24, 2013 at 15:43
  • 1
    $\begingroup$ I added the tags kernel and mathlink as this is a more general problem than .netlink. $\endgroup$
    – rcollyer
    Commented Jul 24, 2013 at 15:52
  • $\begingroup$ 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. $\endgroup$
    – WolframFan
    Commented Jul 25, 2013 at 6:37

2 Answers 2

26
$\begingroup$

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 = MathLink`CallFrontEnd[FrontEnd`GetErrorsInSelectionPacket[nb]]; 
        NotebookClose[nb]
    ];
    pinks
];

which only returns the FE errors. (Edit: I removed the use of Internal`WithLocalSettings 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

MathLink`CallFrontEnd[FrontEnd`GetErrorsInSelectionPacket[nb]]

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

$\endgroup$
14
  • 3
    $\begingroup$ Nice. I wonder how people learn about hidden things like FrontEnd`GetErrorsInSelectionPacket. 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. $\endgroup$ Commented Jul 24, 2013 at 16:27
  • $\begingroup$ @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. $\endgroup$
    – rcollyer
    Commented Jul 24, 2013 at 17:09
  • $\begingroup$ @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. $\endgroup$
    – rcollyer
    Commented Jul 24, 2013 at 17:11
  • $\begingroup$ @JacobAkkerboom I've added a potential fix for the Visible -> False problem. If you would try it again, I would appreciate it. $\endgroup$
    – rcollyer
    Commented Oct 9, 2013 at 12:57
  • $\begingroup$ @rcollyer I think it works fine now :). $\endgroup$ Commented Oct 9, 2013 at 13:44
9
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ There is a method, see my answer. $\endgroup$
    – rcollyer
    Commented Jul 24, 2013 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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