By Maarten Manders

Pimpin Harry’s pretty bluescreen

By Maarten Manders

Proper error and exception display can save you a lot af time and nerves. A few months ago, Harry published a slick bluescreen script for exception handling. I modified it to handle errors as well and added some features which make it useful in productive systems as well:

  • Error logging
  • Error Mailing
  • Configuration

The script logs or mails unique errors only once to prevent your log file or mailbox to be spammed with the same error again and again. It also takes care of the error level including shutup operator. It’s a little bit hacky but did well on our dev servers (where errors tend to happen) in the past few weeks.

To use it, download and include the two files and then setup the error and exception handler in your PHP script:

set_exception_handler(array('ErrorHandler', 'handleException'));
set_error_handler(array('ErrorHandler', 'handleError'));
  • vidluther

    Just to clarify this post. You will need to make sure you “require” these two files as well. Otherwise your code will not know what handleException or handleError are.

    so add

  • vidluther didn’t take my code. try #2.

    require '/path/to/ErrorConfig.class.php';
    require '/path/to/ErrorHandler.class.php';

  • nsbucky

    hmm too bad so much of php land still uses php4? Would you be kind enough to port it or should I?

  • I’m keen to see a port aswell.

  • nsbucky, ccburns: What makes you think that it’s written for PHP 4?

  • random

    Maarten, I think they meant they were hoping for a port *to* PHP4.

  • Would be nice, I’m just not ready to switch to PHP5 right yet :)

  • brossiekoppie


    (PHP 5)
    set_exception_handler — Sets a user-defined exception handler function

    Porting this to PHP4 would be really difficult ;)

  • How long ago did PHP 5 come out…

  • mwolfe

    How long ago did PHP 5 come out…

    must have been 2 years ago or so.. most hosts support it now (at least as an option), you might have to name your files .php5 or something like that..
    I dont see any reason to code in php 4 at all anymore.

  • I dont see any reason to code in php 4 at all anymore.

    Some hosts still don’t support php5 yet. I use hostgator and they’re still stuck on 4. Which sucks because I have 5 on my development server.

  • melitonas

    Will this Custom error handler be called regardless of the silencing operator??

  • Yes, it will be called, but the error will be discarded.

  • melitonas

    But it does not discardes at all…
    I have added these lines:

    public static function handleError($errno, $errstr, $errfile, $errline, $errcontext) {
    /* Check if error needs to be handled */
    if(($errno & ErrorConfig::LEVEL) != $errno) {
    if (error_reporting() == 0) {
    // print "(silenced) ";

    /* Error types */
    $error_types = array(
    1 => 'ERROR',
    2 => 'WARNING',

  • I’m sorry, I thought that I’d committed the fix:

    			 * Check if error needs to be handled.
    			 * Note: error level 0 means we're having a shut-up operator (@)
    			if(($errno & ErrorConfig::LEVEL) != $errno || 0 === error_reporting()) {
  • lsolesen

    Nevermind, found the svn. Though it does not seem as the code is kept up to date? What is the license on the code?

  • Passh Reloaded

    Can you please reupload the two files for this post?


Get the latest in Front-end, once a week, for free.