Name

Checking Errors — How to handle errors from the extension.

Checking errors

When an array of properties is retrieved with the function mapi_getprops by example there could some problems with the properties. Because of optimalization MAPI has a restricition on the size of a property. When by example a PR_BODY is too big the error MAPI_E_NOT_ENOUGH_MEMORY will be returned instead of the actual data.

The type of this property will be set to PT_ERROR instead of PT_TSTRING. The value of the property will be the code of the error that occured. The numeric value of the property tag will be 0x000A0037 which is different from 0x001E0037. This requires a special way to check if there is an error and find out what the error code is.

The following program listing gives an example how to check the property on an error and how to get the error from the property:

Example 2.2.  Checking property errors in an object

// get properties
$msg_props = mapi_getprops($message);
 
if (array_key_exists($msg_props, PR_BODY) {
   // there is a body with no error, display body here
} else {
   $pr_body_error = mapi_prop_tag(PT_ERROR, mapi_prop_id(PR_BODY));
   if (array_key_exists($msg_props, $pr_body_error)) {
      // found an error, now check which
      switch($msg_props[$pr_body_error]) {
         case MAPI_E_NOT_ENOUGH_MEMORY:
            // found but too big, use mapi_openproperty the get the data. 
         break; 
         default:
            // other error. 
         break;
      }
   } else {
      // No error found, the body doesn't exist in this object.
   } 
}
		  

Also, when checking error values (such as 0x80040107, MAPI_E_NOT_FOUND), you must use the mapi_is_error function to check whether the valus is actually an error. This is due to the fact that 0x800xxxxx numbers are not handed as unsigned numbers within PHP.

Example 2.3.  Checking for errors or warnings

if(mapi_is_error($msg_props[$pr_body_error])) {
  echo "error!\n";
}
		  

When a function returns the value 'false', it means the function has failed. To retrieve the MAPI error, you can use the mapi_last_hresult() function, to get the last returned HRESULT value of a MAPI function. Example:

Example 2.4.  Checking last MAPI returned error code

$inbox = mapi_msgstore_getreceivefolder($store);
if ($inbox == false) {
	print "no inbox available!\n";
	printf("last result: %x\n",mapi_last_hresult());
}