How to trigger custom error on fatal errors only

I’d like for my site to send me an email any time there is a fatal error. I’ve been trying different things but can’t figure out how to distinguish warnings and notices from errors. Thank you for your help.

Below is the script I’ve written so far that gathers data with a test error trigger at the bottom.


<?php


//set error handler
set_error_handler("customError");

//error handler function
function customError($errno, $errstr, $errfile, $errline) {

  	/*
  	* GATHER DATA
  	*/

	switch ($errno) {
		case 256:
			$error.= "<b>My ERROR</b> [$errno] $errstr<br />\
";
			$error.= "  Fatal error on line $errline in file $errfile";
			$error.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\
";
			$error.= "Aborting...<br />\
".
			",FILE:  $errfile, LINE: $errline";
	
			break;
	
		case 512:
			$error.= "<b>E_USER_WARNING/b> [$errno] $errstr, FILE:  $errfile, LINE: $errline<br />\
";
			break;
	
		case 1024:
		$error.= "<b>My NOTICE</b> [$errno] $errstr, FILE:  $errfile, LINE: $errline.<br />\
";
		break;
		
		case 2:
		$error.= "<b>E_WARNING</b> [$errno] $errstr, FILE:  $errfile, LINE: $errline<br />\
";
		break;
		
		case 8:
		$error.= "<b>My NOTICE</b> [$errno] $errstr, FILE:  $errfile, LINE: $errline<br />\
";
		break;
		
		case 4096:
		$error.= "<b>E_RECOVERABLE_ERROR</b> [$errno] $errstr, FILE:  $errfile, LINE: $errline<br />\
";
		break;
	
		default:
		$error.= "Unknown error type: [$errno] $errstr, FILE:  $errfile, LINE: $errline<br />\
";
		break;
    }

    if($_SERVER){
		$server= "<h2>SERVER VARIABLES</h2>";
		foreach($_SERVER as $k=>$v){
			$server.="<p><strong>$k:</strong> $v</p>";
		}
    }

    if($_GET){
		$get= "<h2>GET VARIABLES</h2>";
		foreach($_GET as $k=>$v){
			$get.="<p><strong>$k:</strong> $v</p>";
		}
    }

    if($_POST){
		 $post= "<h2>POST VARIABLES</h2>";
		foreach($_POST as $k=>$v){
			$post.="<p><strong>$k:</strong> $v</p>";
		}
    }

    if($_SESSION){
     $session= "<h2>SESSION VARIABLES</h2>";
    	foreach($_SESSION as $k=>$v){
    		$session.="<p><strong>$k:</strong> $v</p>";
    	}
    }

    echo
    "<div style='font-family:arial,verdana;font-size:12px;'>".
    $error.$server.$get.$post.$session.
    "</div>";

    /*
    * MAIL ERROR
    */

    /*
    * REDIRECT PAGE
    */

    exit;

 }


//trigger error

if($_POST){
$sql="SELECT * FROM table";
mysql_query($sql) or die(mysql_error());
}
?>

<form action='?get_variable=1' method='post'>
	<input type='text' value='testing' name='post_variable' />
	<input type='submit' />
</form>

Can’t be done. Fatal error occur outside of the user state so you cannot catch them to throw a custom exception. Sorry :frowning:

Steve

You can use register_shutdown_function for some fatal errors, parse errors cannot ever be caught since you never enter PHP to begin with.

Nice wonshikee!

Found this then … http://www.phpcodebase.com/catch-fatal-errors-in-php/

Interesting. I may have been using the terminology incorrectly here. When I meant “fatal error”, I meant errors that will cause the site to fail like bad mysql queries or missing critical variables, for example.

The register_shutdown_function works great in conjunction with the script above because it catches errors the other doesn’t.

But I’d still like to figure out how to distinguish an error that will cause failure in the first script from one that will be ignored. Any ideas on that?
One possibility would be to make a list of all errors I want to catch and trigger the email only when those are captured, but it seems like there should be a better way.

Thank you E

Thank you.

Hi eruna,

Typically you will use Throw, Try and Catch. Typically Throw (new exception) is used when the state of a PHP object goes wrong is some way; however you can use these in functions like:

  
 function calculateFraction($value){
    if(!$value){
        throw new Exception('Cannot divide by zero. Your passed value is either null or zero.');
    } else {
        return (1/$value);
    }
}
try {
    echo calculateFraction(2);
    echo calculateFraction(0);
 } catch (Exception $e){
    echo 'Caught exception: ',  $e->getMessage(), "\
";
}
/* Outputs */ 
0.5
Caught exception: Cannot divide by zero. Your passed value is either null or zero.?>
 

You can see how the calculateFraction() function raises and error based on an error condition (divide by zero) and throws the exception. In the code where we call the calculateFraction() function we enclose this in a try/catch block that allows us to catch any errors that the code executed in the Try throws an error. We can then control what happens with this error.

Hope this helps,
Steve