Blog Post RSS ?

Blogs » PHP » Pimpin Harry’s pretty bluescreen
 

Pimpin Harry’s pretty bluescreen


  • Save to
    Del.icio.us

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

This post has 17 responses so far

  1. 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

     
  2. Wierd..it didn’t take my code. try #2.

    require '/path/to/ErrorConfig.class.php'; require '/path/to/ErrorHandler.class.php';
     
  3. hmm too bad so much of php land still uses php4? Would you be kind enough to port it or should I?

     
  4. I’m keen to see a port aswell.

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

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

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

     
  8. set_exception_handler

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

    Porting this to PHP4 would be really difficult ;)

     
  9. How long ago did PHP 5 come out…

     
  10. 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.

     
  11. 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.

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

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

     
  14. 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',

     
  15. 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;
    			}
    
    
     
  16. Nevermind, found the svn. Though it does not seem as the code is kept up to date? What is the license on the code?

     
  17. Can you please reupload the two files for this post?

    Thanks

     

Sponsored Links

Leave a response

You are not logged in, log in with your SitePoint Forum username and password.

-OR- Post Anonymously

* Make sure any code samples are escaped (i.e. ‘<b>’ becomes ‘&lt;b&gt;’).

If not logged in, your comments will be placed in a moderation queue. This means your comment may not appear until one of our moderators approves it.

SitePoint Marketplace

Buy and sell Websites, templates, domain names, hosting, graphics and more.

Logo Design, Web page Design and more!

99designs

  • Custom logo designs created ‘just for you’.
  • Pick the design you like best.
  • Only pay if you’re satisfied with the result.