Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more error output for db error in drush #666

Open
wants to merge 1 commit into
base: 7.x-master
Choose a base branch
from

Conversation

eileenmcnaughton
Copy link
Contributor

Without this it just says there was an error - no clue as to what

@civibot
Copy link

civibot bot commented May 18, 2023

(Standard links)

@civibot civibot bot added the 7.x-master label May 18, 2023
catch (API_Exception $e) {
catch (DBQueryException $e) {
drush_set_error('CIVICRM api error', $e->getDBErrorMessage() . ' ' . $e->getUserMessage()
. (in_array('debug=1', $args, TRUE) ? "\n" . $e->getSQL() : '')
Copy link
Contributor

@demeritcowboy demeritcowboy May 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
. (in_array('debug=1', $args, TRUE) ? "\n" . $e->getSQL() : '')
. (!empty($params['debug']) ? ("\n" . $e->getSQL()) : '')

$args has already been parsed and put into params, and depending on how you specify the args it might not look like debug=1. In fact I couldn't even figure out how to use this for api4 without using the json method where it's not in args.

Also for me getSQL() returned blank, but that's not a blocker maybe it depends what the error situation is. For me I forced a db syntax error.

I also get a secondary error Undefined variable $result, but it's not because of the PR. A followup could wrap the output block below to only run if $result actually got set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@demeritcowboy then I think maybe checking debug is too complex & just add the extra output?

@eileenmcnaughton
Copy link
Contributor Author

I just trialled this patch as getting the data better - of course now I found why I had an error I need to switch to solving it

+use Civi\Core\Exception\DBQueryException;
+
 /**
  * Implements hook_drush_command().
  *
@@ -1592,8 +1594,14 @@ function drush_civicrm_api() {
         unset($params['version']);
         $result = civicrm_api3($entity, $action, $params);
       }
-      catch (CiviCRM_API3_Exception $e) {
+      catch (CRM_Core_Exception $e) {
         drush_set_error('CIVICRM api error', $e->getMessage());
+        drush_print('CIVICRM api error', $e->getMessage());
+        $nestedException = $e->getErrorData()['exception'] ?? NULL;
+        if ($nestedException && is_a($nestedException, '\Civi\Core\Exception\DBQueryException')) {
+          drush_set_error('CIVICRM api error', $nestedException->getDBErrorMessage() . ' ' . $nestedException->getUserMessage() . $nestedException->getSQL() ? "\n" . $nestedException->getSQL() : '');
+          drush_print('CIVICRM api error', $nestedException->getDBErrorMessage() . ' ' . $nestedException->getUserMessage() . $nestedException->getSQL() ? "\n" . $nestedException->getSQL() : '');
+        }
       }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants