Custom Error Handling not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AlexC
Forum Commoner
Posts: 83
Joined: Mon May 22, 2006 10:03 am

Custom Error Handling not working

Post by AlexC »

Hey,

I'm tring to set up my own custom error handling, but it's not working - I still get the default PHP errors, why is this?

index.php:

Code: Select all

<?php

// ------------- ~ Tango ~ ------------- \\
// File: index.php
// Author: Alex Cartwright
// Created: 12th November 2006
// ------------- ~ Tango ~ ------------- \\

	// Include lib files
	require_once( 'lib/libgeneral.php' );
	
	// Include required core files
	require_once( 'core/libehc.php' );
	$libehc = new libehc();

	woahError();

?>
core/libehc.php:

Code: Select all

<?php

// ------------- ~ Tango ~ ------------- \\
// File: core/libehc.php
// Author: Alex Cartwright
// Created: 20th November 2006
// ------------- ~ Tango ~ ------------- \\	
	
	// Set Error reporting level
	error_reporting( E_ALL );
			
	class libehc {
	
		var $tangoErrors = NULL;
		
		function libehc() {
			// Take over from PHP's default error handling
			set_error_handler( array( $this, 'errorHandler' ) );
		}	
			
		//---
		// errorHandler() is the main TangoCMS Error Handler
		//---
		function errorHandler( $type, $string, $file, $line, $vars ) {
	
			// Decide what type of error has occured and handel it accordingly
			switch( $type ) {
			
				case E_TANGO:
					$this->tangoError( $string, $file, $line, $vars );
					break;
							
				case E_USER_NOTICE:
				case E_NOTICE:
					$this->errorNotice( $string, $file, $line, $vars );
					break; 
					
				case E_USER_WARNING:
				case E_COMPILE_WARNING:
				case E_CORE_WARNING:
				case E_WARNING:
					$this->errorWarning( $string, $file, $line, $vars );
					break;
					
				case E_USER_ERROR:
				case E_COMPILE_ERROR:
				case E_CORE_ERROR:
				case E_ERROR:
				case E_PARSE:	
				default:
				 	$this->errorFatal( $string, $file, $line, $vars );
					break;
			
			}		
		
		}		
	
		//---
		// tangoError() will store errors into an array
		//---
		function tangoError( $string, $file, $line, $vars ) {
			$this->tangoErrors[] = $string;
		}
		
		//---
		// errorFatal() will display a nice W3C page with a detailed error report then halt the script
		//---
		function errorFatal( $string, $file, $line, $vars ) {
		
			$siteName = isDefined( "_CONFIG_TITLE", 'TangoCMS' );
			
			echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
					<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
					<head>
						<title>'.$siteName.' - Error</title>
					</head>
					<body>
						<strong>A severe error has occured and caused the page to halt:</strong><br />'.$string.'<br /><br />
						<textarea cols="60" rows="10" readonly="readonly" name="error">'.
							$string.', in '.$file.' on line '.$line.'
						</textarea>
						<br /><br />
						Please refresh the page to try again. If the problem continues, we shall try to fix it as soon as possible. Thank you
					</body>
					</html>';
			die();
			
		}
		
		//---
		// errorWarning() will output a detailed error
		//---
		function errorWarning( $string, $file, $line, $vars ) {		
			echo '<strong>Warning:</strong>'.$string.', in '.$file.' on line '.$line;		
		}
		
		//---
		// errorNotice() will output a detailed error
		//---
		function errorNotice( $string, $file, $line, $vars ) {		
			echo '<strong>Notice:</strong>'.$string.', in '.$file.' on line '.$line;		
		}
	
	}
		
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Fatal error: Call to undefined function woahError()
It's an E_ERROR.
http://de2.php.net/set_error_handler wrote:The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.
AlexC
Forum Commoner
Posts: 83
Joined: Mon May 22, 2006 10:03 am

Post by AlexC »

awwww bugger. Ok thanks!
AlexC
Forum Commoner
Posts: 83
Joined: Mon May 22, 2006 10:03 am

Post by AlexC »

Just looking back over this again, why does the PHP manual show their custom error handler, handling errors such as E_ERROR, E_PARSE etc when it clearly can not handle them?

http://uk2.php.net/errorfunc

Code: Select all

$errortype = array (
               E_ERROR              => 'Error',
               E_WARNING            => 'Warning',
               E_PARSE              => 'Parsing Error',
               E_NOTICE            => 'Notice',
               E_CORE_ERROR        => 'Core Error',
               E_CORE_WARNING      => 'Core Warning',
               E_COMPILE_ERROR      => 'Compile Error',
               E_COMPILE_WARNING    => 'Compile Warning',
               E_USER_ERROR        => 'User Error',
               E_USER_WARNING      => 'User Warning',
               E_USER_NOTICE        => 'User Notice',
               E_STRICT            => 'Runtime Notice',
               E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
               );
Is there any way to handle the E_ERROR, Warning, Parse, anything non user errors?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://uk2.php.net/errorfunc
// define an assoc array of error string
// in reality the only entries we should
// consider are
E_WARNING, E_NOTICE, E_USER_ERROR,
// E_USER_WARNING and E_USER_NOTICE
$errortype = array (
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice',
E_RECOVERABLE_ERROR => 'Catchable Fatal Error'
);
Post Reply