Error handling - PHP

This is a discussion on Error handling - PHP ; I am trying to add some error handling to my code. Instead of reinventing the wheel, I searched and found the code below. For the most part, it seems to work. When I have an error, it doesn't get shown. ...

+ Reply to Thread
Results 1 to 3 of 3

Error handling

  1. Default Error handling

    I am trying to add some error handling to my code. Instead of
    reinventing the wheel, I searched and found the code below. For the
    most part, it seems to work. When I have an error, it doesn't get
    shown. However, I don't get any output. I tracked it down to the
    $errno. It is outputting a number where the switch statement is
    expecting words. Short of typing an array converting number to
    words, what am I missing/doing wrong?

    set_error_handler('errorHandler');

    function errorHandler ($errno, $errstr, $errfile, $errline, $errcontext){
    global $query;

    switch ($errno){
    case E_USER_WARNING:
    case E_USER_NOTICE:
    case E_WARNING:
    case E_NOTICE:
    case E_CORE_WARNING:
    case E_COMPILE_WARNING:
    break;
    case E_USER_ERROR:
    case E_ERROR:
    case E_PARSE:
    case E_CORE_ERROR:
    case E_COMPILE_ERROR:

    if(eregi('^(sql)$', $errstr)){
    $MYSQL_ERRNO = mysql_errno();
    $MYSQL_ERROR = mysql_error();
    $errstr = "MySQL error: $MYSQL_ERRNO : $MYSQL_ERROR";
    }else{
    $query = NULL;
    }

    $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 '$errfile'.</p>\n";
    $errorstring .= "<p>Script: '{$_SERVER['PHP_SELF']}'.</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 administrator:</p>\n";
    echo "<b><font color='red'>\n$errorstring\n</b></font>";

    /*error_log($errorstring, 1, $_SERVER['SERVER_ADMIN']);

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

    /*session_start();
    session_unset();
    session_destroy();*/

    die();
    default:
    break;
    }


    # prevent further script execution
    exit();
    }

  2. Default Re: [PHP] Error handling

    E_USER_WARNING is a 'constant' whose value is a number.

    It is not a string.

    If you do this:

    echo "E_USER_WARNING is set to '", E_USER_WARNING, "'<br />\n";

    you will see exactly what number it is.

    So whatever you thought was going wrong, this is not it.

    In addition...

    The check for $query should probably be more like if (strlen($query))
    since it's remotely possible that you could screw up badly enough for
    $query to start with characters which happen to fall in the range of
    0-9, and if that happens to be, say:
    0 and user_id = other_table.user_id
    then this test will fail, despite having a borked $query as the root
    cause of your troubles.

    I've got no idea what that mess of stuff about the errcontext is all
    about, but I suspect that unless you are ALSO using their same
    object-oriented error handling modules, and have included the source
    for that, then your error handler is probably puking on itself, and
    PHP is probably refusing to do infinite recursion to call the error
    handler from within the error handler. Or it *IS* doing exactly that,
    and spiking your CPU before it chokes on the time limit (or even
    memory limit) in php.ini

    If that's not the problem, then I don't see anything here, other than
    some icky code style, that is wrong.

    On Thu, March 1, 2007 3:24 pm, Chris Ditty wrote:
    > I am trying to add some error handling to my code. Instead of
    > reinventing the wheel, I searched and found the code below. For the
    > most part, it seems to work. When I have an error, it doesn't get
    > shown. However, I don't get any output. I tracked it down to the
    > $errno. It is outputting a number where the switch statement is
    > expecting words. Short of typing an array converting number to
    > words, what am I missing/doing wrong?
    >
    > set_error_handler('errorHandler');
    >
    > function errorHandler ($errno, $errstr, $errfile, $errline,
    > $errcontext){
    > global $query;
    >
    > switch ($errno){
    > case E_USER_WARNING:
    > case E_USER_NOTICE:
    > case E_WARNING:
    > case E_NOTICE:
    > case E_CORE_WARNING:
    > case E_COMPILE_WARNING:
    > break;
    > case E_USER_ERROR:
    > case E_ERROR:
    > case E_PARSE:
    > case E_CORE_ERROR:
    > case E_COMPILE_ERROR:
    >
    > if(eregi('^(sql)$', $errstr)){
    > $MYSQL_ERRNO = mysql_errno();
    > $MYSQL_ERROR = mysql_error();
    > $errstr = "MySQL error: $MYSQL_ERRNO : $MYSQL_ERROR";
    > }else{
    > $query = NULL;
    > }
    >
    > $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
    > '$errfile'.</p>\n";
    > $errorstring .= "<p>Script: '{$_SERVER['PHP_SELF']}'.</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
    > administrator:</p>\n";
    > echo "<b><font color='red'>\n$errorstring\n</b></font>";
    >
    > /*error_log($errorstring, 1, $_SERVER['SERVER_ADMIN']);
    >
    > $logfile = $_SERVER['DOCUMENT_ROOT'] .'/errorlog.html';
    > error_log($errorstring, 3, $logfile);*/
    >
    > /*session_start();
    > session_unset();
    > session_destroy();*/
    >
    > die();
    > default:
    > break;
    > }
    >
    >
    > # prevent further script execution
    > exit();
    > }
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    >



    --
    Some people have a "gift" link here.
    Know what I want?
    I want you to buy a CD from some starving artist.
    http://cdbaby.com/browse/from/lynch
    Yeah, I get a buck. So?

  3. Default Re: Error handling

    Chris Ditty wrote:
    > I am trying to add some error handling to my code. Instead of
    > reinventing the wheel, I searched and found the code below. For the
    > most part, it seems to work. When I have an error, it doesn't get
    > shown. However, I don't get any output. I tracked it down to the
    > $errno. It is outputting a number where the switch statement is
    > expecting words. Short of typing an array converting number to
    > words, what am I missing/doing wrong?


    You also want to consider just giving the user a generic error message
    and saving the full detailed error for the admin/developer. The details
    in error messages are often useful in finding vulnerabilities in an app.

    Roberto

+ Reply to Thread