1

I've been doing some research on php error handling as well as exception handling.

For instance, to handle user errors, it is best to use set_error_handler() for user errors. Example code:

// Destinations
define("ADMIN_EMAIL", "nobody@stanford.edu"); 
define("LOG_FILE", "/my/home/errors.log");

// Destination types
define("DEST_EMAIL", "1");
define("DEST_LOGFILE", "3");

/**
* my_error_handler($errno, $errstr, $errfile, $errline)
*
* Author(s): thanosb, ddonahue
* Date: May 11, 2008
* 
* custom error handler
*
* Parameters:
*  $errno:   Error level
*  $errstr:  Error message
*  $errfile: File in which the error was raised
*  $errline: Line at which the error occurred
*/

function my_error_handler($errno, $errstr, $errfile, $errline)
{  
 switch ($errno) {
 case E_USER_ERROR:
  // Send an e-mail to the administrator
  error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL);

  // Write the error to our log file
  error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_LOGFILE, LOG_FILE);
  break;

case E_USER_WARNING:
  // Write the error to our log file
  error_log("Warning: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
  break;

case E_USER_NOTICE:
  // Write the error to our log file
  error_log("Notice: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
  break;

default:
  // Write the error to our log file
  error_log("Unknown error [#$errno]: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
  break;
}

// Don't execute PHP's internal error handler
return TRUE;
}


// Use set_error_handler() to tell PHP to use our method
$old_error_handler = set_error_handler("my_error_handler");

Code found at http://www.stanford.edu/dept/its/communications/webservices/wiki/index.php/How_to_perform_error_handling_in_PHP.

Then for fatal errors:

register_shutdown_function('handleShutdown');

function handleShutdown() {
    $error = error_get_last();
    if($error !== NULL){
        $info = "[SHUTDOWN] file:".$error['file']." | ln:".$error['line']." |    msg:".$error['message'] .PHP_EOL;
        yourPrintOrMailFunction($info);
    }
    else{
        yourPrintOrMailFunction("SHUTDOWN");
    }
}

Code found at How do I catch a PHP Fatal Error.

From what I can tell, it appears that this will cover essentially all errors that can occur within a script (as a very general statement - it's hard to predict all errors). When developing a class, thought, it seems that it is suggested to use exceptions to handle them internally.

My question is whether or not these examples are generally considered appropriate to handle errors in a production setting, or if anything is blatantly incorrect or lacking.

In any case, are these functions attached to all files or set within a class?

Any help is appreciated.

Edit: I also meant to add, how would I stop a script if a certain error is encountered. Obviously, exit() or die() does the trick, but is there anything more appropriate to do so?

Community
  • 1
  • 1
Mlagma
  • 1,240
  • 2
  • 21
  • 49

3 Answers3

1

My question is whether or not these examples are generally considered appropriate to handle errors in a production setting, or if anything is blatantly incorrect or lacking.

Yes it is. You just have to be sure that you catch ALL generated exception. I also want to remember you that if there is a synthax error in your scrip, or if your server is bad configured, php could output errors that are not handled because they are compile time errors.

n any case, are these functions attached to all files or set within a class?

Once you registered them (with set_error_handler) they are applied to every files that are called in your script apart from if one of your file redifine the handler.

and to stop a script, exit is the good way

artragis
  • 3,677
  • 1
  • 18
  • 30
  • Very nice, I believe the example error handling had a default in the switch that caught unexpected errors. And add a default exception handler would be simple to do. – Mlagma Sep 23 '12 at 19:13
0

There are approaches to handle all errors as exceptions, and that's fine, I've done so on one or two projects and I'm satisfied with the results.

However do note this, even Warnings and Notices will become fatal (as Exceptions are fatal if not catch'ed). Also, syntax errors won't be handled as Exceptions, as they are not run-time errors, but compile-time (meaning your script never compiled, and no lines of code were run).

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

My question is whether or not these examples are generally considered appropriate to handle errors in a production setting, or if anything is blatantly incorrect or lacking.

PHP Errors Yes .. they are good you just need better storage for your errors especially if you are working with multiple servers

Exceptions I don't think this is sufficient ... They Need to be handled depending on type eg Database , Network , etc.

Baba
  • 94,024
  • 28
  • 166
  • 217