Had a issue recently when my MySQL host went away. Even though I have all PHP and Apache error's set to not display on screen I received an friendly Drupal error page stating "Error" and then "The website encountered an unexpected error. Please try again later.". Below that text however was a red error box stating "PDOException: SQLSTATE[HY000] [2005] Unknown MySQL server host" and showing the server path and file to the lock.inc file as well. I have not found a way to hide this message.

For a production site, this is not good to show any server information and would prefer having the option to take a user to a backup site or static page with a custom error message. With some exploring and trail/error I came up with this solution. It hacks the core includes/error.inc file (I know, not good to do) but seems to work nicely in a "fatal" error.

In includes/error.inc around line 241 I added the following if statement.

if ($fatal) {
      global $conf; //added to get global config info
     // add this if to check for a value in settings.php and if so redirect user
      if (isset($conf['site_fatal_alt_url'])) {
        header('Location: '. $conf['site_fatal_alt_url']);
      }
...
}

In settings.php I added:

/** 
 * If site has a fatal error, such as failure to connect to database
 * can specify a new location to take visitors. Can be either full
 * url or site path http://<new_url>.com/ or /<path_to_static_file>/
 */
$conf['site_fatal_alt_url'] = '/offline/static.html';

New to Drupal so would appreciate any input on this as a new core feature, or if it can be somehow be turned into a module or if there is just something I'm missing elsewhere in Drupal config.

Thanks.

Comments

trevorw’s picture

Issue summary: View changes

wrapped code in code tags

ElemAm024’s picture

THANK YOU so much for this, you just saved my life.

Tharick’s picture

For Drupal 8?

rajesh.vishwakarma’s picture

I have manage mysql connection fail/gone away error using-

try {
  require_once DRUPAL_ROOT . '/includes/database/database.inc';
  Database::getConnection();
}
  catch (Exception $e) {
  header("Location: $base_url/YOUR_PAGE.html");
  die;
}
capellic’s picture

Consider using this module (https://www.drupal.org/project/hide_php_fatal_error) instead of hacking core?

Version: 7.23 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.