Custom Error Handler - When is it active?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am trying to understand when a custom error handler kicks in. I am
expecting that the custom handler would kick in for both the following
errors. This is the example code from an index.php file.  I've left
out the header and include for the error handler.



echo $_SERVER['PHP_SELF'] . "<br />";
echo $_SERVER['SERVER_ADMIN'] . "<br />";
echo $_SERVER['DOCUMENT_ROOT'] . "<br />";

//trigger_error("Testing error handler", E_USER_ERROR);



If I uncomment the trigger _error statement, the custom error handler
works fine.  However, if I leave the trigger_error commented out and
remove the semi-colon at the end of the second echo statement, I get a
standard parse error, not the custom error handler.

Code for the error handler follows:


function errorHandler ($errno, $errstr, $errfile, $errline,
$errcontext) {
  switch ($errno) {
    case E_USER_WARNING:
    case E_USER_NOTICE:
    case E_WARNING:
    case E_NOTICE:
    case E_CORE_WARNING:
    case E_USER_ERROR:
    case E_ERROR:
    case E_PARSE:
    case E_CORE_ERROR:

      global $query;

      /* Get extra info if SQL error  */
      if (eregi('^(sql)$', $errstr)) {
        $MYSQL_ERRNO = mysql_errno();
        $MYSQL_ERROR = mysql_error();
        $errstr = "MySQL error: $MYSQL_ERRNO : $MYSQL_ERROR";
      else {
        $query = NULL;
      /* define the error string */
      $errorstring = "<h2>" .date('Y-m-d H:i:s') ."</h2>\n";
      $errorstring .= "<p>Fatal Error: $errstr (# $errno).</p>\n";

      if ($query) {
        $errorstring .= "<p>SQL query: $query</p>\n";
      $errorstring .= "<p>Error in line $errline of file
      $errorstring .= "<p>Script: ''.</p>\n";

      if (isset($errcontext['this'])) {
        if (is_object($errcontext['this'])) {
          $classname = get_class($errcontext['this']);
          $parentclass = get_parent_class($errcontext['this']);
          $errorstring .= "<p>Object/Class: '$classname', Parent
Class: '$parentclass'.</p>\n";

       echo "<h2>This system is temporarily unavailable</h2>\n";
       echo "<p>The following has been reported to the
       echo "<b><font color='red'>\n$errorstring\n</b></font>";
       error_log($errorstring, 1, $_SERVER['SERVER_ADMIN'],"From:");

       $logfile = $_SERVER['DOCUMENT_ROOT'] .'/errorlog.html';
       error_log($errorstring, 3, $logfile);




Re: Custom Error Handler - When is it active?

Call Me Tom wrote:
Quoted text here. Click to load it

Syntax errors occur during the parsing of your script, before any code
is executed. So, set_error_handler() function has not been called when
the syntax error occurs.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Custom Error Handler - When is it active?

Call Me Tom escribió:
Quoted text here. Click to load it

The reference is here (see the top "Description" box):

If example rises a E_PARSE error, you can't catch it.

The thumb rule is that, since your custom error handler is PHP code,
your script must be in a runnable state so it can execute your error
handling code.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor satinado:

Site Timeline