Skip to content

Commit

Permalink
ManagedEntities::onApiError - Log extension name and exception details
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Jul 21, 2023
1 parent 591d7ab commit 319166f
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions CRM/Core/ManagedEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function get($moduleName, $name) {
$result = civicrm_api3($dao->entity_type, 'getsingle', $params);
}
catch (Exception $e) {
$this->onApiError($dao->name, 'getsingle', $result['error_message']);
$this->onApiError($dao->module, $dao->name, 'getsingle', $result['error_message'], $e);
}
return $result;
}
Expand Down Expand Up @@ -186,7 +186,7 @@ protected function insertNewEntity(array $item) {
$result = civicrm_api4($item['entity_type'], 'save', $params);
}
catch (CRM_Core_Exception $e) {
$this->onApiError($item['name'], 'save', $e->getMessage());
$this->onApiError($item['module'], $item['name'], 'save', $e->getMessage(), $e);
return;
}
$id = $result->first()['id'];
Expand All @@ -195,7 +195,7 @@ protected function insertNewEntity(array $item) {
else {
$result = civicrm_api($item['entity_type'], 'create', $params);
if (!empty($result['is_error'])) {
$this->onApiError($item['name'], 'create', $result['error_message']);
$this->onApiError($item['module'], $item['name'], 'create', $result['error_message']);
return;
}
$id = $result['id'];
Expand Down Expand Up @@ -254,7 +254,7 @@ private function updateExistingEntity(array $item) {

$result = civicrm_api($item['entity_type'], 'create', $params);
if ($result['is_error']) {
$this->onApiError($item['name'], 'create', $result['error_message']);
$this->onApiError($item['module'], $item['name'], 'create', $result['error_message']);
return;
}
}
Expand All @@ -267,7 +267,7 @@ private function updateExistingEntity(array $item) {
civicrm_api4($item['entity_type'], 'update', $params);
}
catch (CRM_Core_Exception $e) {
$this->onApiError($item['name'], 'update', $e->getMessage());
$this->onApiError($item['module'], $item['name'], 'update', $e->getMessage(), $e);
return;
}
}
Expand Down Expand Up @@ -301,7 +301,7 @@ protected function disableEntity(array $item): void {
];
$result = civicrm_api($item['entity_type'], 'create', $params);
if ($result['is_error']) {
$this->onApiError($item['name'], 'create', $result['error_message']);
$this->onApiError($item['module'], $item['name'], 'create', $result['error_message']);
return;
}
// Reset the `entity_modified_date` timestamp to indicate that the entity has not been modified by the user.
Expand Down Expand Up @@ -371,7 +371,7 @@ protected function removeStaleEntity(array $item) {
}
}
catch (CRM_Core_Exception $e) {
$this->onApiError($item['name'], 'delete', $e->getMessage());
$this->onApiError($item['module'], $item['name'], 'delete', $e->getMessage(), $e);
}
// Ensure managed record is deleted.
// Note: in many cases CRM_Core_BAO_Managed::on_hook_civicrm_post() will take care of
Expand Down Expand Up @@ -475,23 +475,22 @@ protected function isModuleDisabled(string $module): bool {
}

/**
* @param string $moduleName
* @param string $managedEntityName
* @param string $actionName
* @param string $errorMessage
*
* @throws CRM_Core_Exception
* @param Throwable|null $exception
*/
protected function onApiError(string $managedEntityName, string $actionName, string $errorMessage): void {
$message = "Unable to $actionName managed entity '$managedEntityName'. $errorMessage";
protected function onApiError(string $moduleName, string $managedEntityName, string $actionName, string $errorMessage, ?Throwable $exception = NULL): void {
// During upgrade this problem might be due to an about-to-be-installed extension
// So only log the error if it persists outside of upgrade mode
if (!CRM_Core_Config::isUpgradeMode()) {
Civi::log()->error($message);
}
// Errors should be very loud when debugging
if (Civi::settings()->get('debug_enabled')) {
throw new CRM_Core_Exception($message);
if (CRM_Core_Config::isUpgradeMode()) {
return;
}

$message = sprintf('(%s) Unable to %s managed entity "%s": %s', $moduleName, $actionName, $managedEntityName, $errorMessage);
$context = $exception ? ['exception' => $exception] : [];
Civi::log()->error($message, $context);
}

/**
Expand Down

0 comments on commit 319166f

Please sign in to comment.