Pimpin Harry’s pretty bluescreen

By | | PHP

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

Written By:

Maarten Manders

Maarten is a Web developer studying computer science at the Swiss Federal Institute of Technology (ETH) in Zurich. He enjoys combining PHP with new ideas and proven concepts from different fields of software engineering. Nevertheless, Maarten pays attention to keeping things practical and consistent. He also contributes to the SitePoint Blogs.

Website
>> More Posts By Maarten Manders

 

{ 17 comments }

Passh Reloaded August 27, 2007 at 10:53 am

Can you please reupload the two files for this post?

Thanks

lsolesen March 28, 2007 at 4:34 pm

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

Maarten Manders September 20, 2006 at 10:17 pm

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;
			}

melitonas September 20, 2006 at 8:05 pm

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

Maarten Manders September 20, 2006 at 7:57 pm

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

melitonas September 20, 2006 at 7:52 pm

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

busy September 3, 2006 at 5:40 am

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.

mwolfe August 22, 2006 at 1:31 am

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.

TheLunchBox August 19, 2006 at 5:27 am

How long ago did PHP 5 come out…

brossiekoppie August 14, 2006 at 11:00 pm

set_exception_handler

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

Porting this to PHP4 would be really difficult ;)

Dean C August 14, 2006 at 3:03 am

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

random August 13, 2006 at 11:08 pm

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

Maarten Manders August 13, 2006 at 6:56 am

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

ccburns August 12, 2006 at 8:16 pm

I’m keen to see a port aswell.

nsbucky August 12, 2006 at 2:22 pm

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

vidluther August 12, 2006 at 8:07 am

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


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

vidluther August 12, 2006 at 8:06 am

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

Comments on this entry are closed.