Jump to content

Error Handler Issue


Altec

Recommended Posts

I've started redesigning my site and I started working on a more robust (haha) error handler, more so than I usually do. I'm trying to distinguish between the different error levels using switch(). This is what I have:

 

function error_handler($error_lvl,$error_msg,$error_file,$error_line)
{
    switch($error_lvl)
    {
        case E_USER_NOTICE:
            $level = 'Notice';
        break;
        case E_USER_WARNING:
            $level = 'Warning';
        break;
        case E_USER_ERROR:
            $level = 'Error';
        break;
        default:
            $level = 'Unknown: '.$error_lvl;
    }
    $date = '['.date('d/m/y H:i P').'] ';
    $pad = floor((strlen($date) / 2));
    $error_msg = preg_replace('/ \[<a.*>\]/i','',$error_msg);
    
    $error = $date.$level."\n";
    $error .= str_repeat(' ',$pad).wordwrap('Message: '.$error_msg,75,"\n".str_repeat(' ',$pad+9))."\n";
    $error .= str_repeat(' ',$pad).wordwrap('File: '.$error_file,75,"\n".str_repeat(' ',$pad+6))."\n";
    $error .= str_repeat(' ',$pad).wordwrap('Line: '.$error_line,75,"\n".str_repeat(' ',$pad+6))."\n\n";
    
    echo '<div class="fatal_error">Uh Oh! Something terrible has happened! We\'ve dispatched our in-house monkey to take a look at things.</div></body></html>';
    @file_put_contents('../pyg.log.txt',$error,FILE_APPEND);
    exit;
}

I'm initiating the error like so:

 

$db_handle = @mysql_connect('localhost','root','notmypassword');
if( !$db_handle )
{
    trigger_error('MySQL: '.mysql_error(),E_USER_ERROR);
}

Obviously I'm passing a bad password to mysql_connect. Anyway, I trigger the error at E_USER_ERROR which should return a value of 256 (caught by case E_USER_ERROR) but instead returns E_ERROR (integer value 2), as shown in the log:

 

[28/02/10 03:28 -06:00] Unknown: 2
            Message: mysql_connect(): Access denied for user 'root'@'localhost'
                     (using password: YES)
            File: /home/root/public_html/projects/inc/init.php
            Line: 5

 

Custom error handlers should NOT handle E_ERROR level errors as they are initiated before script execution:

 

The following error types cannot be handled with a user defined    function: E_ERROR' date=' [b']E_PARSE[/b],    E_CORE_ERROR, E_CORE_WARNING,    E_COMPILE_ERROR,    E_COMPILE_WARNING, and    most of E_STRICT raised in the file  where    set_error_handler() is called.

What's going on? Is it a problem with my PHP configuration?

Link to comment
https://forums.phpfreaks.com/topic/193641-error-handler-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.