WEBMATHEMATICA TUTORIAL

Handling Errors

There are a number of ways that webMathematica computations can lead to an error. An input might fail the security test, the computation might take too long so that the Mathematica kernel is restarted, or there might be some type of page logic error. You can learn about certain types of errors and, often, solve them using the logging system. However, it is not possible to avoid all errors, for example you cannot predict all inputs to the server, so some of them might fail the security test. In these cases you might want to customize the way the error is handled.

When an error is generated webMathematica deals with it in one of two ways. For serious errors that require the Mathematica kernel to be restarted, the request results in an HTTP error of status 403, indicating that the server could not complete the request. Alternatively, other errors result in a Mathematica exception being thrown and this leads to a normal page being returned, but with some special text being inserted for the error.

If you want to customize the handling of these errors, you can do this with Mathematica code to catch the Mathematica exceptions and by adding an error page to handle the HTTP error.

Catching Mathematica Error Exceptions

webMathematica throws errors in Mathematica as MSPException expressions, and you can add code to catch these. The exceptions that can be caught are listed below.

MSPException["ParseError"]if the value cannot be interpreted by Mathematica
MSPException["SecurityError"]if the value does not pass the security test
MSPException["ValueError"]if the value is not a string, this indicates a programmatic error by the page author
MSPException["VariableError"]if the variable is not a Mathematica symbol, this indicates a programmatic error by the page author
MSPException["NoValueError"]if a variable has no value
MSPException["VersionError"]if a version mismatch problem is found

Some sample code that uses Catch to catch a security exception is shown below.

<msp:evaluate>
Catch[ expr, MSPException["SecurityError"], errorFunction]
</msp:evaluate>

A sample implementation for errorFunction is shown below. As you can see, its signature gets two arguments; the actual exception expression is the second argument.

errorFunction[_, MSPException[ "SecurityError"]] :=
tidyUpSecurityError[]

Adding an HTTP Error Page

You can catch the HTTP errors by adding an error page. This can be done by adding code to the web.xml file that is found in the WEB-INF folder. The following should redirect to a page /Resources/Tools/Error.jsp; in fact, this is a sample page found in the webMathematica layout.

<error-page>
<error-code>403</error-code>
<location>/Resources/Tools/Error.jsp</location>
</error-page>

Inside the error page you need to reset the error. You can also get an integral listing of the error and an error string. Sample code is shown in the following.

<%
response.setStatus(200);
Object type = (Object)request.getAttribute(
com.wolfram.msp.MSPStatics.MSPErrorType);
String text = (String)request.getAttribute(
com.wolfram.msp.MSPStatics.MSPErrorText);
%>