Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My own custom error handler

Status
Not open for further replies.

mervgh

IS-IT--Management
Jul 3, 2003
1
GB
I am trying to follow a script for creating my own error handler however I am expecting "A system error occurred. We apologise for the inconvenience." instead I get the following, why am I getting a fatal error surely it should just be another "A system error occurred. We apologise for the inconvenience." message ?


A system error occurred. We apologise for the inconvenience.

A system error occurred. We apologise for the inconvenience.

Fatal error: Uncaught DivisionByZeroError: Division by zero in /var/ Stack trace: #0 {main} thrown in /var/ on line 29


I am using php 8.1.14, the example in the book originally had 5 values for my_error_handler however I see that in php 8 it has been reduced to 4. Any help appreciated, thank you


<?php

define('LIVE',TRUE);
$e_vars = 'ok';

// create the error handler
function my_error_handler($e_number,$e_message,$e_file,$e_line) {

// build the message
$message ="An error occurred in script '$e_file' on line $e_line : $e_message\n";

// append $e_vars to $message
$message .= print_r($e_message,1);

if(!LIVE){
echo '<pre>'.$message."\n";
debug_print_backtrace();
echo '</pre><br />';

} else {
echo '<div class="error">A system error occurred. We apologise for the inconvenience.</div><br />';
}
}

set_error_handler('my_error_handler');

// create errors
foreach ($vars as $v){}
$result = 1/0;
?>
 
I deleted my previous answer, as it was "not even wrong" (i.e. more than just wrong) but beside the point.

The usual way to not display error messages in a release or "live" application, besides other differences of php behavior during development and in production is done by using different php.ini files.

PHP installation come with a php.ini that defaults to the development configuration.
There also is php.ini-development and php.ini-production

Also look into the discussion and user contributed notes, that's always a good recommendation to learn most important things aside from the mere technical documentation. It's not kept secret that not all error types cannot be handled with a user defined function, the last paragraph tells that a lot of 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 independent of where they were raised, and most of E_STRICT raised in the file where set_error_handler() is called.

Besides that a division by zeror causes an exception.

See the most upvoted user contributed note by Philip to see that you need more than just youw own error handler. He does not only seet a new error handler, also a new exception handler and also registers a shutdown function.

I'd not even go that route, you should read up on recommendations for production configuration. Open up both nin files and read the comments in them, that's most eye opening about many more things than just error reporting differences. Also you learn that turning off error_reporting does not turn of error logging, which is what surely helps to see errors that occurred in the live system to fix problems. I have not established own error and exception handling even in some larger projects, as the standards that exist are quite sufficient already.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top