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
Comment #0.0
trevorw CreditAttribution: trevorw commentedwrapped code in code tags
Comment #1
ElemAm024 CreditAttribution: ElemAm024 commentedTHANK YOU so much for this, you just saved my life.
Comment #2
Tharick CreditAttribution: Tharick commentedFor Drupal 8?
Comment #3
rajesh.vishwakarma CreditAttribution: rajesh.vishwakarma as a volunteer commentedI have manage mysql connection fail/gone away error using-
Comment #4
capellicConsider using this module (https://www.drupal.org/project/hide_php_fatal_error) instead of hacking core?