SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    936
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to trigger custom error on fatal errors only

    I'd like for my site to send me an email any time there is a fatal error. I've been trying different things but can't figure out how to distinguish warnings and notices from errors. Thank you for your help.

    Below is the script I've written so far that gathers data with a test error trigger at the bottom.

    PHP Code:
    <?php


    //set error handler
    set_error_handler("customError");

    //error handler function
    function customError($errno$errstr$errfile$errline) {
      
          
    /*
          * GATHER DATA
          */
      
        
    switch ($errno) {
            case 
    256:
                
    $error.= "<b>My ERROR</b> [$errno$errstr<br />\n";
                
    $error.= "  Fatal error on line $errline in file $errfile";
                
    $error.= ", PHP " PHP_VERSION " (" PHP_OS ")<br />\n";
                
    $error.= "Aborting...<br />\n".
                
    ",FILE:  $errfile, LINE: $errline";
        
                break;
        
            case 
    512:
                
    $error.= "<b>E_USER_WARNING/b> [$errno$errstr, FILE:  $errfile, LINE: $errline<br />\n";
                break;
        
            case 
    1024:
            
    $error.= "<b>My NOTICE</b> [$errno$errstr, FILE:  $errfile, LINE: $errline.<br />\n";
            break;
            
            case 
    2:
            
    $error.= "<b>E_WARNING</b> [$errno$errstr, FILE:  $errfile, LINE: $errline<br />\n";
            break;
            
            case 
    8:
            
    $error.= "<b>My NOTICE</b> [$errno$errstr, FILE:  $errfile, LINE: $errline<br />\n";
            break;
            
            case 
    4096:
            
    $error.= "<b>E_RECOVERABLE_ERROR</b> [$errno$errstr, FILE:  $errfile, LINE: $errline<br />\n";
            break;
        
            default:
            
    $error.= "Unknown error type: [$errno$errstr, FILE:  $errfile, LINE: $errline<br />\n";
            break;
        }

        if(
    $_SERVER){
            
    $server"<h2>SERVER VARIABLES</h2>";
            foreach(
    $_SERVER as $k=>$v){
                
    $server.="<p><strong>$k:</strong> $v</p>";
            }
        }
        
        if(
    $_GET){
            
    $get"<h2>GET VARIABLES</h2>";
            foreach(
    $_GET as $k=>$v){
                
    $get.="<p><strong>$k:</strong> $v</p>";
            }
        }
        
        if(
    $_POST){
             
    $post"<h2>POST VARIABLES</h2>";
            foreach(
    $_POST as $k=>$v){
                
    $post.="<p><strong>$k:</strong> $v</p>";
            }
        }
        
        if(
    $_SESSION){
         
    $session"<h2>SESSION VARIABLES</h2>";
            foreach(
    $_SESSION as $k=>$v){
                
    $session.="<p><strong>$k:</strong> $v</p>";
            }
        }
        
        echo 
        
    "<div style='font-family:arial,verdana;font-size:12px;'>".
        
    $error.$server.$get.$post.$session.
        
    "</div>";
        
        
    /*
        * MAIL ERROR
        */
        
        /*
        * REDIRECT PAGE
        */
        
        
    exit;

     }


    //trigger error

    if($_POST){
    $sql="SELECT * FROM table";
    mysql_query($sql) or die(mysql_error());
    }
    ?>

    <form action='?get_variable=1' method='post'>
        <input type='text' value='testing' name='post_variable' />
        <input type='submit' />
    </form>

  2. #2
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Can't be done. Fatal error occur outside of the user state so you cannot catch them to throw a custom exception. Sorry

    Steve
    ictus==""

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You can use register_shutdown_function for some fatal errors, parse errors cannot ever be caught since you never enter PHP to begin with.

  4. #4
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Nice wonshikee!

    Found this then ... http://www.phpcodebase.com/catch-fatal-errors-in-php/
    ictus==""

  5. #5
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    936
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Interesting. I may have been using the terminology incorrectly here. When I meant "fatal error", I meant errors that will cause the site to fail like bad mysql queries or missing critical variables, for example.

    The register_shutdown_function works great in conjunction with the script above because it catches errors the other doesn't.

    But I'd still like to figure out how to distinguish an error that will cause failure in the first script from one that will be ignored. Any ideas on that?
    One possibility would be to make a list of all errors I want to catch and trigger the email only when those are captured, but it seems like there should be a better way.

    Thank you E

    Thank you.

  6. #6
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Hi eruna,

    Typically you will use Throw, Try and Catch. Typically Throw (new exception) is used when the state of a PHP object goes wrong is some way; however you can use these in functions like:

    PHP Code:
     function calculateFraction($value){
        if(!
    $value){
            throw new 
    Exception('Cannot divide by zero. Your passed value is either null or zero.');
        } else {
            return (
    1/$value);
        }
    }
    try {
        echo 
    calculateFraction(2);
        echo 
    calculateFraction(0);
     } catch (
    Exception $e){
        echo 
    'Caught exception: ',  $e->getMessage(), "\n";
    }
    /* Outputs */ 
    0.5
    Caught exception
    Cannot divide by zeroYour passed value is either null or zero.?> 
    You can see how the calculateFraction() function raises and error based on an error condition (divide by zero) and throws the exception. In the code where we call the calculateFraction() function we enclose this in a try/catch block that allows us to catch any errors that the code executed in the Try throws an error. We can then control what happens with this error.

    Hope this helps,
    Steve
    ictus==""


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •