Skip to content

Commit

Permalink
APIv3 - Use the Exception rather than the rule
Browse files Browse the repository at this point in the history
Following up on #19323 this converts APIv3 in Smarty and Ajax to use exceptions instead
of overriding the PEAR exception handler. Now that PEAR throws exceptions there is
no need to do so and it was interfering with try/catch handlers within the api call.
  • Loading branch information
colemanw committed Nov 15, 2022
1 parent 1231d25 commit 6e00ea9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
15 changes: 5 additions & 10 deletions CRM/Core/Smarty/plugins/function.crmAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,17 @@ function smarty_function_crmAPI($params, &$smarty) {
$smarty->trigger_error("assign: missing 'entity' parameter");
return "crmAPI: missing 'entity' parameter";
}
$errorScope = CRM_Core_TemporaryErrorScope::create(['CRM_Utils_REST', 'fatal']);
$entity = $params['entity'];
$action = CRM_Utils_Array::value('action', $params, 'get');
$params['sequential'] = CRM_Utils_Array::value('sequential', $params, 1);
$var = $params['var'] ?? NULL;
CRM_Utils_Array::remove($params, 'entity', 'action', 'var');
$params['version'] = 3;
require_once 'api/api.php';
$result = civicrm_api($entity, $action, $params);
unset($errorScope);
if ($result === FALSE) {
$smarty->trigger_error("Unknown error");
try {
$result = civicrm_api3($entity, $action, $params);
}

if (!empty($result['is_error'])) {
$smarty->trigger_error("{crmAPI} " . $result["error_message"]);
catch (Exception $e) {
$smarty->trigger_error('{crmAPI} ' . $e->getMessage());
return NULL;
}

if (!$var) {
Expand Down
14 changes: 5 additions & 9 deletions CRM/Core/Smarty/plugins/function.crmSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,14 @@
* @return int|string|null
*/
function smarty_function_crmSetting($params, &$smarty) {

$errorScope = CRM_Core_TemporaryErrorScope::create(['CRM_Utils_REST', 'fatal']);
unset($params['method']);
unset($params['assign']);
$params['version'] = 3;

require_once 'api/api.php';
$result = civicrm_api('setting', 'getvalue', $params);
unset($errorScope);
// Core-688 FALSE is returned by Boolean settings, thus giving false errors.
if ($result === NULL) {
$smarty->trigger_error("Unknown error");
try {
$result = civicrm_api3('setting', 'getvalue', $params);
}
catch (Exception $e) {
$smarty->trigger_error('{crmAPI} ' . $e->getMessage());
return NULL;
}

Expand Down
36 changes: 15 additions & 21 deletions CRM/Utils/REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,6 @@ public static function process(&$args, $params) {
$params['version'] = 3;
}

if ($params['version'] == 2) {
$result['is_error'] = 1;
$result['error_message'] = "FATAL: API v2 not accessible from ajax/REST";
$result['deprecated'] = "Please upgrade to API v3";
return $result;
}

if ($_SERVER['REQUEST_METHOD'] == 'GET' &&
strtolower(substr($args[2], 0, 3)) != 'get' &&
strtolower($args[2] != 'check')) {
Expand All @@ -293,12 +286,12 @@ public static function process(&$args, $params) {
}

// trap all fatal errors
$errorScope = CRM_Core_TemporaryErrorScope::create([
'CRM_Utils_REST',
'fatal',
]);
$result = civicrm_api($args[1], $args[2], $params);
unset($errorScope);
try {
$result = civicrm_api($args[1], $args[2], $params);
}
catch (Exception $e) {
return self::error($e->getMessage());
}

if ($result === FALSE) {
return self::error('Unknown error.');
Expand Down Expand Up @@ -346,7 +339,9 @@ public static function &buildParamList() {
}

/**
* @param $pearError
* Unused function from the dark ages before PHP Exceptions
* @deprecated
* @param PEAR_Error $pearError
*/
public static function fatal($pearError) {
CRM_Utils_System::setHttpHeader('Content-Type', 'text/xml');
Expand Down Expand Up @@ -462,20 +457,19 @@ public static function ajaxJson() {
}

$params['check_permissions'] = TRUE;
$params['version'] = 3;
// $requestParams is local-only; this line seems pointless unless there's a side-effect influencing other functions
$_GET['json'] = $requestParams['json'] = 1;
if (!$params['sequential']) {
$params['sequential'] = 1;
}

// trap all fatal errors
$errorScope = CRM_Core_TemporaryErrorScope::create([
'CRM_Utils_REST',
'fatal',
]);
$result = civicrm_api($entity, $action, $params);
unset($errorScope);
try {
$result = civicrm_api3($entity, $action, $params);
}
catch (Exception $e) {
$result = self::error($e->getMessage());
}

echo self::output($result);

Expand Down

0 comments on commit 6e00ea9

Please sign in to comment.