raytri Posted June 1, 2010 Share Posted June 1, 2010 I'm just starting to explore custom error-handling routines. I've written the following, and it works. The idea is to make sure error messages aren't show to the user unless there is a fatal error, and then it's a custom message. Meanwhile, the detailed error message is e-mailed to me, both for fatal errors and warnings. Is this a good approach for error-handling code? Or am I missing something big? (I know there's some redundancy, like I could combine two of the "if" statements in the customError() function, and I'm pretty sure that any error capable of causing a shutdown is by definition serious, so the "if" statement in the fatalErrorShutdownHandler() function is unnecessary. But is the basic structure sound?) <?php //Don't show errors to user ini_set('display_errors', 0); //Trigger customer error function when a fatal error aborts the script register_shutdown_function('fatalErrorShutdownHandler'); // the function function customError($errno, $errstr, $errfile, $errline) { //For fatal errors, display custom message if($errno < 4) {ob_end_clean(); ?> <h1>Error loading page</h1> <p>The website encountered an error loading your page. The Website administrator has been notified; please try reloading the page. If that doesn't work, please try again later.</p> <p>If the error persists, please notify us at <a href="[email protected]">[email protected]</a></p> <?php } //Report both fatal errors and warnings if($errno < { //Include full URI of page that triggered the error $badpage = $_SERVER['REQUEST_URI']; error_log("Error: [$errno] $errstr in $errfile on line $errline. \n\n URI is $badpage",1, "[email protected]","From: [email protected]");} //If it's a fatal error, kill the rest of the page if($errno < 4) {die();} } //Triggers error reporting in the event of a fatal error shutdown function fatalErrorShutdownHandler() { //gets error that caused the shutdown $error = error_get_last(); //Only sends error message for serious errors if ($error['type'] < { customError($error['type'], $error['message'], $error['file'], $error['line']); } } //sets custom error handler for non-fatal errors set_error_handler("customError"); //buffer page output so errors can be caught before anything is output to page ob_start(); ?> Link to comment https://forums.phpfreaks.com/topic/203532-error-handling-code/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.