3

Due to a server problem one of our production sites was unable to connect to the DB.

While it was unable to connect the site was outputting the error:

An exception has been thrown during the rendering of a template ("Craft can't connect to the database with the credentials in craft/config/db.php.") in "_layout" at line 10.

I'm a little concerned that a production site is outputting this kind of information, is there a way to stop these errors being output?

(The site is currently running Craft Pro 2.1.2561 with dev mode disabled.)

Thanks.

3

DB connection errors are the only case where Craft will output the full error even if Dev Mode is disabled, because 99.99% of the time those only occur when Craft is not yet installed, and it’s an extremely common issue people run into when installing Craft.

If you really want to prevent those errors from showing up on the front end, you can edit your index.php file so that it catches them, and displays something different:

<?php

// Path to your craft/ folder
$craftPath = '../craft';

// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';

if (!is_file($path))
{
    exit('Could not find your craft/ folder. Please ensure that <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}

try
{
    require_once $path;
}
catch (Craft\DbConnectException $e)
{
    exit('An error occurred.');
}
1
  • Thanks. In the end I went with the heavy handed approach of catching all to make sure nothing sneaks through. I found the DB Exception was only thrown when accessing domain.com/admin but I was still getting the original message when accessing domain.com as it is throwing a Craft HTTP Exception. – fcb Aug 21 '14 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.