Main Content

Handle Function Processing Errors

To handle errors that occur when processing MATLAB® functions:

  1. Evaluate the status returned by the feval() function to determine if the function was successfully processed.

  2. Get the error information using the getLastErrorInfo() function.

  3. Interrogate the type field of the error detail to determine the type of error.

  4. Process the error information appropriately.

  5. Clean-up the resources used by the error information using the destroyLastErrorInfo() function.

Determine if an Error Occurred

The feval() function returns a value of type mpsStatus, which signifies if an error occurred while the function was being processed. The status can have one of two values:

  • MPS_OK indicates that the function processed successfully.

  • MPS_FAILURE indicates that an error occurred.

For example, to check if an error occurred while evaluating a MATLAB function, use an if-then statement.

status = mpsruntime->feval(context,funUrl,outArgs,outVal,inArgs,inVal);
if (status==MPS_OK)
{
  ...
}
else
{
  ...
}

Get the Error Information

If a call to the feval() function returns a value of MPS_FAILURE, you can get the details of the error by calling the getLastErrorInfo() function. It returns an mpsErrorInfo structure that contains these fields:

  • message — String containing general information about the error

  • type — Character identifying the type of error. The type identifier is used to select the correct data type for the detailed error information.

  • details — Structure containing details, such as the MATLAB stack, about the error and its underlying cause

To get the error information and print the basic error message:

mpsErrorInfo error;
mpsruntime->getLastErrorInfo(context, &error);
std::cout << "Error: " << error.message << std::endl;

Determine the Type of Error

Before you can process the detailed error information, you need to determine what type of error occurred. This is done by interrogating the type field of the mpsErrorInfo structure. It can have one of three values:

  • MPS_HTTP_ERROR_INFO — Non-200 HTTP error occurred and the details are stored in an mpsErrorInfoHTTP structure

  • MPS_MATLAB_ERROR_INFO — MATLAB error occurred and the details are stored in an mpsErrorInfoMATLAB structure

  • MPS_GENERIC_ERROR_INFO — Indeterminate error occurred and the details are stored in an mpsErrorInfoGeneric structure

Once you determine the type of error, you can process the detailed information. To determine the error type using a switch statement:

mpsErrorInfo error;
mpsruntime->getLastErrorInfo(context, &error);
switch(error.type)
{
case MPS_HTTP_ERROR_INFO:
  ...
case MPS_MATLAB_ERROR_INFO:
  ...
case MPS_MATLAB_ERROR_INFO:
  ...
}

Process HTTP Errors

The details of an HTTP errors are stored in an mpsErrorInfoHTTP structure. This structure has two fields:

  • responseCode — HTTP error code

  • responseMessage — String containing the message returned with the error

For example, if you attempt to access a function using an invalid URL, the client may return an mpsErrorInfoHTTP structure with the following values:

  • responseCode — 404

  • responseMessage — Not Found

Process MATLAB Errors

If the error occurs while the MATLAB Runtime is evaluating the function the client returns an mpsErrorInfoMATLAB structure. This structure has the following fields:

  • message — Error message returned by the MATLAB Runtime

  • identifier — MATLAB error ID

  • matlabStackMATLAB Runtime stack

  • matlabStackDepth — Number of entries in the MATLAB Runtime stack

The entries in the MATLAB Runtime stack have the following fields:

  • file — Name of the MATLAB file that caused the error

  • function — Name of the MATLAB function that caused the error

  • line — Line number in the MATLAB file that caused the error

To print the contents of a MATLAB error:

mpsErrorInfo error;
mpsruntime->getLastErrorInfo(context, &error);
switch(error.type)
{
case MPS_HTTP_ERROR_INFO:
  ...
case MPS_MATLAB_ERROR_INFO:
  std::cout << "MATLAB: " << error.details.matlab.identifier
    << std::endl;
  std::cout << error.details.matlab.message << std::endl;
  for (int i=0; i < error.details.matlab.matlabStackDepth; i++)
  {
    std::cout << "in " <<  error.details.matlab.matlabStack[i].file 
     << " at " << error.details.matlab.matlabStack[i].function 
     << " line number " << error.details.matlab.matlabStack[i].line
     << std::endl;
  }
case MPS_MATLAB_ERROR_INFO:
  ...
}

Process Generic Errors

If an error other than a non-200 HTTP response or a MATLAB Runtime exception occurs, the client returns an mpsErrorInfoGeneric structure containing a genericErrorMessage field.

Clean Up Error Information

The error information created by the MATLAB Production Server™ client runtime is opaque. Once you have processed the error, clean up the resources used by the error using the mpsClientRuntime destroyLastErrorInfo() function. It takes a pointer to the error information returned from getLastErrorInfo().

mpsClientRuntime* mpsruntime = mpsInitialize();
mpsErrorInfo error;
mpsruntime->getLastErrorInfo(context, &error);
...
mpsruntime->destroyLastErrorInfo(&error);