Pimpin Harry’s pretty bluescreen

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'));
 

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • 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

    Wierd..it 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?

  • http://www.altstudio.com.au ccburns

    I’m keen to see a port aswell.

  • http://www.phpism.net Maarten Manders

    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.

  • http://www.deanclatworthy.com Dean C

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

  • brossiekoppie

    set_exception_handler

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

    Porting this to PHP4 would be really difficult ;)

  • http://ian.sundermedia.com TheLunchBox

    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.

  • http://www.digitalgreenlight.com busy

    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??

  • http://www.phpism.net Maarten Manders

    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) {
    return;
    }
    if (error_reporting() == 0) {
    // print "(silenced) ";
    return;
    }

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

  • http://www.phpism.net Maarten Manders

    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()) {
    				return;
    			}
    
    
  • 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?

    Thanks