Allow extensions to skip entity operations by catching exceptions thrown in pre and post hooks
Overview
Extensions should be able to skip actions performed on entities with a warning/error message. hook_civicrm_pre()
and hook_civicrm_post()
implementations do not allow for skipping altogether, but only for altering parameters.
Example use-case
The de.systopia.donrec extension forbids contributions be deleted when they have been legally receipted, since allowing deletion of such contributions would be considered fraudulent behavior by tax authorities.
Current behaviour
Currently, exceptions thrown in implementations of those hooks produce fatal errors, which, when run via Ajax, appear to "hang the system", because the XHR returns a 500. There is no way for extensions to skip such an action in a way that would be handeled by the UI.
Proposed behaviour
hook_civicrm_pre()
and hook_civicrm_post()
implementations should be able to skip execution of the current action (e.g. deleting an entity). The invoking code should therefore catch exceptions thrown by those implementations. This would allow for displaying exception messages as error message popups in the UI.
Comments
I'm not sure whether this can be done generically or would have to be implemented separately for each caller. I guess it's the latter. In that case, consider this issue as a discussion starter for whether this is desirable to include in CiviCRM Core.