From 9c465c3b95240a7623f510ba5e3557ce6200cb3f Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 25 Mar 2014 23:09:19 -0700 Subject: [PATCH] civicrm_api3_create_error - Simplify --- Civi/API/Kernel.php | 36 +++++++++++++++++++++++++++++++++--- api/v3/Activity.php | 2 +- api/v3/utils.php | 22 +++------------------- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/Civi/API/Kernel.php b/Civi/API/Kernel.php index 9740466574..69f96b8da9 100644 --- a/Civi/API/Kernel.php +++ b/Civi/API/Kernel.php @@ -241,7 +241,7 @@ class Kernel { if (!empty($apiRequest['params']['debug'])) { $data['trace'] = $e->getTraceAsString(); } - return civicrm_api3_create_error($e->getMessage(), $data, $apiRequest, $e->getCode()); + return $this->createError($e->getMessage(), $data, $apiRequest, $e->getCode()); } /** @@ -260,7 +260,7 @@ class Kernel { $data['trace'] = $e->getTraceAsString(); } - return civicrm_api3_create_error($e->getMessage(), $data, $apiRequest, $e->getCode()); + return $this->createError($e->getMessage(), $data, $apiRequest, $e->getCode()); } /** @@ -288,7 +288,37 @@ class Kernel { $data['tip'] = "add debug=1 to your API call to have more info about the error"; } - return civicrm_api3_create_error($e->getMessage(), $data, $apiRequest); + return $this->createError($e->getMessage(), $data, $apiRequest); + } + + /** + * + * @param $data + * @param array $data + * @param object $apiRequest DAO / BAO object to be freed here + * + * @throws API_Exception + * @return array + */ + function createError($msg, $data, $apiRequest, $code = NULL) { + // FIXME what to do with $code? + if ($msg == 'DB Error: constraint violation' || substr($msg, 0, 9) == 'DB Error:' || $msg == 'DB Error: already exists') { + try { + $fields = _civicrm_api3_api_getfields($apiRequest); + _civicrm_api3_validate_fields($apiRequest['entity'], $apiRequest['action'], $apiRequest['params'], $fields, TRUE); + } catch (Exception $e) { + $msg = $e->getMessage(); + } + } + + $data = civicrm_api3_create_error($msg, $data); + + if (isset($apiRequest['params']) && is_array($apiRequest['params']) && !empty($apiRequest['params']['api.has_parent'])) { + $errorCode = empty($data['error_code']) ? 'chained_api_failed' : $data['error_code']; + throw new \API_Exception('Error in call to ' . $apiRequest['entity'] . '_' . $apiRequest['action'] . ' : ' . $msg, $errorCode, $data); + } + + return $data; } /** diff --git a/api/v3/Activity.php b/api/v3/Activity.php index 88b2a7a274..ddade21598 100644 --- a/api/v3/Activity.php +++ b/api/v3/Activity.php @@ -114,7 +114,7 @@ function civicrm_api3_activity_create($params) { if (is_object($activityDAO)) { $activityDAO->free(); } - return civicrm_api3_create_error(ts("Unable to revision existing case activity."), NULL, $activityDAO); + return civicrm_api3_create_error(ts("Unable to revision existing case activity.")); } $createRevision = TRUE; } diff --git a/api/v3/utils.php b/api/v3/utils.php index 860668a1b6..657011b4b5 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -131,36 +131,20 @@ function civicrm_api3_verify_mandatory($params, $daoName = NULL, $keys = array() * * @param $data * @param array $data - * @param array $dao (misnomer) apiRequest which led to this error (with keys "entity", "action", etc) * * @throws API_Exception * @return array */ -function civicrm_api3_create_error($msg, $data = array(), &$dao = NULL) { - if (is_array($dao)) { - if ($msg == 'DB Error: constraint violation' || substr($msg, 0,9) == 'DB Error:' || $msg == 'DB Error: already exists') { - try { - $fields = _civicrm_api3_api_getfields($dao); - _civicrm_api3_validate_fields($dao['entity'], $dao['action'], $dao['params'], $fields, TRUE); - } - catch(Exception $e) { - $msg = $e->getMessage(); - } - } - } +function civicrm_api3_create_error($msg, $data = array()) { $data['is_error'] = 1; $data['error_message'] = $msg; - // we will show sql to privelledged user only (not sure of a specific - // security hole here but seems sensible - perhaps should apply to the trace as well? + // we will show sql to privileged user only (not sure of a specific + // security hole here but seems sensible - perhaps should apply to the trace as well?) if(isset($data['sql']) && CRM_Core_Permission::check('Administer CiviCRM')) { $data['debug_information'] = $data['sql']; // Isn't this redundant? } else { unset($data['sql']); } - if (is_array($dao) && isset($dao['params']) && is_array($dao['params']) && !empty($dao['params']['api.has_parent'])) { - $errorCode = empty($data['error_code']) ? 'chained_api_failed' : $data['error_code']; - throw new API_Exception('Error in call to ' . $dao['entity'] . '_' . $dao['action'] . ' : ' . $msg, $errorCode, $data); - } return $data; } -- 2.25.1