✨️ improve exception handling and API response
This commit is contained in:
parent
ab8c16fac2
commit
060f7ec2dc
2 changed files with 38 additions and 12 deletions
|
@ -405,6 +405,9 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
} elseif ($curl_status_code == 500) {
|
} elseif ($curl_status_code == 500) {
|
||||||
throw new Exception('https status code 500 (internal error)');
|
throw new Exception('https status code 500 (internal error)');
|
||||||
}
|
}
|
||||||
|
if (sizeof($response) == 1 && isset($response['message'])){
|
||||||
|
throw new Exception($response['message']);
|
||||||
|
}
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -140,7 +140,7 @@ function civicrm_api3_twingle_project_Sync(array $params): array {
|
||||||
else {
|
else {
|
||||||
|
|
||||||
// Counter for sync errors
|
// Counter for sync errors
|
||||||
$errors_occurred = 0;
|
$errors = [];
|
||||||
|
|
||||||
// Get all projects from Twingle
|
// Get all projects from Twingle
|
||||||
$projects_from_twingle = $twingleApi->getProject();
|
$projects_from_twingle = $twingleApi->getProject();
|
||||||
|
@ -172,7 +172,7 @@ function civicrm_api3_twingle_project_Sync(array $params): array {
|
||||||
// push project to Twingle
|
// push project to Twingle
|
||||||
$result = _pushProjectToTwingle($project, $twingleApi, $params);
|
$result = _pushProjectToTwingle($project, $twingleApi, $params);
|
||||||
if ($result['is_error'] != 0) {
|
if ($result['is_error'] != 0) {
|
||||||
$errors_occurred++;
|
$errors[$result['id']] = $result['error_message'];
|
||||||
$returnValues[$project->getId()] =
|
$returnValues[$project->getId()] =
|
||||||
$project->getResponse($result['error_message']);
|
$project->getResponse($result['error_message']);
|
||||||
}
|
}
|
||||||
|
@ -199,7 +199,7 @@ function civicrm_api3_twingle_project_Sync(array $params): array {
|
||||||
$returnValues[$project->getId()] =
|
$returnValues[$project->getId()] =
|
||||||
$project->getResponse('TwingleProject created');
|
$project->getResponse('TwingleProject created');
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$errors_occurred++;
|
$errors[$result['id']] = $result['error_message'];
|
||||||
Civi::log()->error(
|
Civi::log()->error(
|
||||||
E::LONG_NAME .
|
E::LONG_NAME .
|
||||||
' could not create TwingleProject: ' .
|
' could not create TwingleProject: ' .
|
||||||
|
@ -245,13 +245,29 @@ function civicrm_api3_twingle_project_Sync(array $params): array {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return results
|
// Return results
|
||||||
if ($errors_occurred > 0) {
|
if (sizeof($errors) > 0) {
|
||||||
$errorMessage = ($errors_occurred > 1)
|
|
||||||
? "$errors_occurred synchronisation processes resulted with an error"
|
$errorCount = sizeof($errors);
|
||||||
|
$errorMessage = ($errorCount > 1)
|
||||||
|
? "$errorCount synchronisation processes resulted with an error"
|
||||||
: "1 synchronisation process resulted with an error";
|
: "1 synchronisation process resulted with an error";
|
||||||
|
|
||||||
|
// Log errors
|
||||||
|
Civi::log()->error(E::LONG_NAME . ': ' . $errorMessage, $errors);
|
||||||
|
|
||||||
|
// Return API Error
|
||||||
|
$errorMessage = $errorMessage . ': [';
|
||||||
|
foreach ($errors as $key => $value) {
|
||||||
|
$errorMessage =
|
||||||
|
$errorMessage .
|
||||||
|
" ['project_id' => '$key', 'error_message' => '$value'],";
|
||||||
|
}
|
||||||
|
$errorMessage =
|
||||||
|
substr($errorMessage, 0, strlen($errorMessage) - 1) . ' ]';
|
||||||
|
|
||||||
return civicrm_api3_create_error(
|
return civicrm_api3_create_error(
|
||||||
$errorMessage,
|
$errorMessage,
|
||||||
$returnValues
|
$errors
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -286,8 +302,10 @@ function _updateProjectLocally(array $project_from_twingle,
|
||||||
|
|
||||||
// If this is a test, do not make db changes
|
// If this is a test, do not make db changes
|
||||||
if (array_key_exists('is_test', $params) && $params['is_test']) {
|
if (array_key_exists('is_test', $params) && $params['is_test']) {
|
||||||
|
$response[$project->getId()] =
|
||||||
|
$project->getResponse('TwingleProject ready to update');
|
||||||
return civicrm_api3_create_success(
|
return civicrm_api3_create_success(
|
||||||
$project->getResponse('TwingleProject ready to update'),
|
$response,
|
||||||
$params,
|
$params,
|
||||||
'TwingleProject',
|
'TwingleProject',
|
||||||
'Sync'
|
'Sync'
|
||||||
|
@ -295,7 +313,8 @@ function _updateProjectLocally(array $project_from_twingle,
|
||||||
}
|
}
|
||||||
// ... else, update local TwingleProject campaign
|
// ... else, update local TwingleProject campaign
|
||||||
$project->create(TRUE);
|
$project->create(TRUE);
|
||||||
$response = $project->getResponse('TwingleProject updated successfully');
|
$response[$project->getId()] =
|
||||||
|
$project->getResponse('TwingleProject updated successfully');
|
||||||
return civicrm_api3_create_success(
|
return civicrm_api3_create_success(
|
||||||
$response,
|
$response,
|
||||||
$params,
|
$params,
|
||||||
|
@ -333,8 +352,10 @@ function _pushProjectToTwingle(TwingleProject $project,
|
||||||
|
|
||||||
// If this is a test, do not make db changes
|
// If this is a test, do not make db changes
|
||||||
if ($params['is_test']) {
|
if ($params['is_test']) {
|
||||||
|
$response[$project->getId] =
|
||||||
|
$project->getResponse('TwingleProject ready to push to Twingle');
|
||||||
return civicrm_api3_create_success(
|
return civicrm_api3_create_success(
|
||||||
$project->getResponse('TwingleProject ready to push to Twingle'),
|
$response,
|
||||||
$params,
|
$params,
|
||||||
'TwingleProject',
|
'TwingleProject',
|
||||||
'Sync'
|
'Sync'
|
||||||
|
@ -363,7 +384,8 @@ function _pushProjectToTwingle(TwingleProject $project,
|
||||||
try {
|
try {
|
||||||
// Create updated campaign
|
// Create updated campaign
|
||||||
$project->create(TRUE);
|
$project->create(TRUE);
|
||||||
$response = $project->getResponse('TwingleProject pushed to Twingle');
|
$response[$project->getId()] =
|
||||||
|
$project->getResponse('TwingleProject pushed to Twingle');
|
||||||
return civicrm_api3_create_success(
|
return civicrm_api3_create_success(
|
||||||
$response,
|
$response,
|
||||||
$params,
|
$params,
|
||||||
|
@ -441,7 +463,8 @@ function _projectSync(TwingleProject $project,
|
||||||
|
|
||||||
// If both versions are still synchronized
|
// If both versions are still synchronized
|
||||||
else {
|
else {
|
||||||
$response = $project->getResponse('TwingleProject up to date');
|
$response[$project->getId()] =
|
||||||
|
$project->getResponse('TwingleProject up to date');
|
||||||
return civicrm_api3_create_success(
|
return civicrm_api3_create_success(
|
||||||
$response,
|
$response,
|
||||||
$params,
|
$params,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue