NotionCommotion Posted May 12, 2015 Share Posted May 12, 2015 Hoping to get some advise regarding handling errors. Consider the following script. Is there any reason to explicit log errors in my_shutdown_handler? They seem to get logged automatically using the default error message. Is it typical to redirect to some static page such as error.html which politely informs the user that an error occurred, or should I be doing something else? Any other recommended changes to this script? <?php define('DEBUG',0); class start { public function __construct() { ini_set('display_errors', 0); error_reporting(E_ALL); register_shutdown_function(array($this,"my_shutdown_handler")); set_error_handler(array($this,"my_error_handler")); set_exception_handler(array($this,"my_exception_handler")); } public function my_shutdown_handler() { $error = error_get_last(); if ($error['type'] == E_ERROR) { $message = "An error occurred in script '{$error['file']}' on line {$error['line']}: {$error['message']}."; error_log ($message); //Is this necessary, or is it automatically called with critical errors? //Email error if desired if (DEBUG) {exit($message);} else {header('Location: /error.html');exit;} //Maybe don't go to a separate URL? } } public function my_error_handler($e_number, $e_message, $e_file, $e_line, $e_vars) { if (ini_get('error_reporting') != 0) { //Will be 0 if ignored using @ $message = "An error occurred in script '$e_file' on line $e_line: $e_message (error no: $e_number)"; error_log ($message); //Email error if desired if (DEBUG) {exit($message);} elseif ( ($e_number != E_NOTICE) && ($e_number < 2048)) {header('Location: /error.html');exit;} } } public function my_exception_handler($e) { $message = "An error occurred in script '{$e->getFile()}' on line {$e->getLine()}: {$e->getMessage()}. Exception code: {$e->getCode()}."; error_log ($message); //Email error if desired if (DEBUG) {exit($message);} else {header('Location: /error.html');exit;} } } $obj=new start(); //$x=$y; //throw new Exception('Division by zero.'); $obj->missingMethod(); echo('<p>done</p>'); ?> Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted May 12, 2015 Share Posted May 12, 2015 If you need to ask the question the you most like don't need to be doing it. Custom error management is -normaly- used to handle errors with a controlled formatting regardless of the webserver environment. This is particularly helpfull when you have an abstact debuging system in place. Also if error logging is turned off on the server then you still have something that you can work with, although it's more common to hold the error info in a database than a flat file and include an email or other alert that can tell you or A.N Other admin about the issue. As for the nice page versus other options : it's scenario specific. Some times it's more fitting to return end users to the last page they were on and offer them some snippet of info telling them that something happend with the last attempted action and that admin have been informed, other times that's not really practical and you need to scratch the whole process and crash them onto an error page. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 12, 2015 Author Share Posted May 12, 2015 Thanks Muddy, Whether a DB or a flat file, I still need to detect the error and run some script, no? So I would just replace error_log ($message); with a query. Am I missing something? In regards to whether I display a nice page or don't even inform the user something went wrong, I am only displaying the nice page if it is a fatal error, uncaught exception, or normal error if not a notice or greater than 2047. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted May 12, 2015 Share Posted May 12, 2015 I believe you'd need to use register_shutdown_function() to capture FATAL errors, which are the most important to know about Otherwise fatal errors will crash the system and won't get reported by a custom error handler because PHP is done processing at that point since fatal errors kill the script right then and there so they can't be logged normally. http://stackoverflow.com/questions/4410632/handle-fatal-errors-in-php-using-register-shutdown-function Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 13, 2015 Author Share Posted May 13, 2015 Thanks CroNIX, Yes, I am doing so in my original post. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.