Jump to content

Custom Error Handler not working for Parse Errors

- - - - -

  • Please log in to reply
1 reply to this topic

#1
RHochstenbach

RHochstenbach

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
I've been trying to create custom PHP error handler to rewrite all errors into a more readable form. I came up with this:

<?php


	//error handler function

	function customError($errno, $errstr, $errfile, $errline, $errcontext) {

 			

 			// If connection to the server is impossible

 			if ($errno == '2') {

 	

 	echo "<p class='error'>Can not connect to the Database!</p>";

 	

 				

				} else { // If some other error occurs

echo "<p class='error'>An error has occurred.</p><p>[".$errno ."] - ".$errstr ."</p>";

			}


	}




//set error handler

set_error_handler("customError");




?>

Basically it checks the error, if it's error number 2 (can't connect to the MySQL database), it shows "Can not Connect to the database". In all other cases it should show "An error has occurred".

This works for most errors. But for some reason it doesn't work for Parse Errors. I trigger such an error by not ending a statement with a semicolon:
echo $myvariable

Instead of showing the text "An Error has occurred", it shows a 'normal' PHP error message:
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in error.php on line 66

Is there a way to also intercept Parse Errors?

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Maybe. I read that you can use register_shutdown_function to catch fatal errors like parse errors.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users