One of the more common errors I've seen with drush, is trying to operate a command on a bootstrapped site, that then proceeds to spew a bunch of HTML containing an error message, and exit with the failed to bootstrap error message.

The proposed error handling API has included an error status for specifically this case.

The way that drush currently detects the bootstrap issue is through a combination of drush_drupal_bootstrap() and drush_shutdown().

Drush attempts to do a drupal_bootstrap_full, and because a failed drupal bootstrap always does an exit(), it checks for a DRUSH_DRUPAL_BOOTSTRAPPED constant that will only be set if the bootstrap completed successfully.

This still has the problem that it dumps HTML to your terminal.

However, through a combination of output logging and the error logging API above, we will be able to work around it.

What I suggest, is instead of heading directly for DRUPAL_BOOTSTRAP_FULL, we initiate the database first, and then FULL.

I also suggest splitting them into separate functions , for instance :


function drush_drupal_bootstrap_full() {
  ob_start();
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  ob_end_clean();
  define('DRUSH_DRUPAL_BOOTSTRAP_FULL', TRUE);
}

function drush_drupal_bootstrap_db() {
  ob_start();
  drupal_bootstrap(DRUPAL_BOOTSTRAP_DB);
  ob_end_clean();
  define('DRUSH_DRUPAL_BOOTSTRAP_DB', TRUE);
}

The drush_shutdown function can now check for these statuses separately.

function drush_shutdown() {
  if (!defined('DRUSH_DRUPAL_BOOTSTRAP_DB')) {
    ob_end_clean();
    drush_set_error(DRUSH_DRUPAL_DB_ERROR);
  }
  elseif (!defined('DRUSH_DRUPAL_BOOTSTRAP_FULL')) {
    ob_end_clean();
    drush_set_error(DRUSH_DRUPAL_BOOTSTRAP_ERROR);
  }
  $error = drush_get_error();
  exit(($error) ? $error : DRUSH_SUCCESS);
}

What is also useful about these functions now, is that any commands that do not bootstrap drupal to operate, now have an
api of fault tolerant functions that they can call.

Because we now use exit codes, if you write a script which calls drush to do something, you can discern whether it was successful, and if not, why not.

This is important if we ever want to see a re-usable update or install command for drush.

I can roll a patch for this, but my solution depends on the proposed error handling API.

Comments

moshe weitzman’s picture

Sounds terrific to me.

adrian’s picture

Status: Active » Fixed

Committed to HEAD.

Also made the help text much clearer for both db error and bootstrap errors.

They now display the database credentials used to attempt a connection, and the drupal root / site path.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.