Friendly Database Errors In Wordpress

Standard Wordpress Error

I’ve just been thinking about the error reporting in Wordpress, prompted by flicking through 37signal’s Defensive Design For the Web.

Although Wordpress comes with a customisable template for the more common 404 errors, there may also be the odd occasion when a visitor receives a database connection error. These errors are usually handled by a standard WordPress error message, which details the problem and suggests a visit to the Wordpress Support Forums.

Although this is probably fine for the webmaster, it’s pretty useless to your visitors,as they’re not in a position to do anything about it.

So next time your database dies under the pressure of all your traffic (or in my case, loses a database connection because of a dodgy host), why not supply your visitors with a friendlier message, explaining that there has been a problem, that you’re working on it, and if applicable, a phone number, other contact details etc.?

How to configure the friendlier messages

In order to replace the standard message, you need to have a dig around in your Wordpress includes directory. In the wp-db.php file, there’s the bail() function (probably towards the bottom of the file). You can easily alter the HTML in here as you wish.

Rather than telling the visitor exactly what the problem is (which will probably confuse them anyway), I decided to take the $message variable output (which outputs the nature of the error), and put it into a mail() function to send me the error message in an email.

I’ve only implemented this in the most recent verion of WordPress (2.0), although I think it’s probably the same for previous versions. Also, it’s worth remembering that as this is a hack to a core WordPress file, you should document this fact somewhere, as it may get over-written at the next WordPress upgrade. If anyone knows a way that this could be built into a plugin, please let me know.

This entry was posted on Monday, February 6th, 2006 at 10:06 pm and is filed under Web. You can follow any responses to this entry through the RSS 2.0 feed. You can make a comment, or trackback from your own site.

3 Comments to “Friendly Database Errors In Wordpress”

  1. Tom, February 7th, 2006 at 9:44 pm

    It’s a shame that when the error message is displayed, WordPress doesent bother to send an email out. Perhaps you could wrap these two ideas into a plugin?

  2. Rob Lewis, February 8th, 2006 at 4:03 pm

    Yeah, this should be definite plugin material, another item to go onto the todo list!

  3. Tom Jemmett, February 10th, 2006 at 1:03 pm

    A slightley better way of doing this could be to use Pear Error class. It allows things such as outputting errors into the syslog and some other stuff.

Leave a Comment