Skip to content

Update newly added API: Return an array on success and streamline error handling

DaveD requested to merge DaveD/cdntaxreceipts:api-error-handling into master

@JonGold This is my proposal to streamline the error handling and at the same time I went ahead and updated it to return an array for values instead of true/false.

(Aside: I'd forgotten how api3 converts any exception you throw, so this is the simplest I can do I think.)

So a typical call would look like this.

try {
 ...
} catch (CRM_Core_Exception $e) {
  switch ($e->getErrorCode()) {
    case CDNTAXRECEIPTS_API_NOTFOUND:
    case CDNTAXRECEIPTS_API_NOEMAIL:
      // we care about these - inform user or whatever
      break;
    default:
      // we don't care if ineligible or email somehow failed
      break;
  }
}

If for example someone wanted to handle ineligible, they could add a case CDNTAXRECEIPTS_API_INELIGIBLE.

On success the values array is the log record, keyed on either the receipt_id, or 0 if you requested sequential, the same as many api calls. See updated success test.

Merge request reports