2

I'm using the following code to catch uncaught exceptions and errors,

function my_exception_handler($e) {
        $dataToStore = array("error" => $e, "server" => $_SERVER, "request" => $_REQUEST, "backtrace" => debug_backtrace());

                //Store $dataToStore in a file
}

function my_error_handler($no, $str, $file, $line) {
    $e = new ErrorException($str, $no, 0, $file, $line);
    my_exception_handler($e);
}

set_error_handler('my_error_handler');
set_exception_handler('my_exception_handler');

I was wondering if there is a way to make this store only FATAL ERRORS in a file, the $e array has a severity which is always 0 apparently.

1
  • 4
    You could always parse the error_log and grep for fatals. Commented Apr 25, 2012 at 20:35

3 Answers 3

1

You need register_shutdown_function for this task:

register_shutdown_function(function() {
  $err = error_get_last(); 

  if(!is_null($err)) {
     if ($err['type'] == E_ERROR || $err['type'] == E_CORE_ERROR) { // extend if you want
       // write to file..
     }
  }
}); 

// test it with
ini_set('max_execution_time', 1); 
sleep(5); 

$err['type'] can consist of constants defined on this page: Error Handling > Predefined Constants

For further information see: Catching fatal errors in PHP

Sign up to request clarification or add additional context in comments.

1 Comment

How does your code snippet differentiate between fatals and warnings?
0
function my_error_handler($no, $str, $file, $line) {
    switch($no){
        case E_CORE_ERROR:    
            $e = new ErrorException($str, $no, 0, $file, $line);
            my_exception_handler($e);
            break;
    }
}

Comments

0

According to PHP's documentation on set_error_handler, the specified handler should be of the following form:

handler ( int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]] )

(…)

The first parameter, errno, contains the level of the error raised, as an integer.

This means you should make your error handler this way:

<?php

function my_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
    // Only process fatal errors
    if ($errno == E_ERROR) {
        // Do your error processing
    }
}

set_error_handler('my_error_handler');

?>

Using this method you can precisely control how you handle each sort of error.


If you only want to process the fatal errors, you could do is much easier:

mixed set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] )

The second parameter, $error_types, allows you to specify which errors should be processed with your custom error handler. You can just pass the E_ERROR constant, like this:

<?php

// Use your existing function

set_error_handler('my_error_handler', E_ERROR);

?>

1 Comment

Your logic makes sense but set_error_handler seems to ignore Fatal Errors, it catches everything but Fatal errors, by that I mean when a Fatal Error occurs, my_error_handler is not called, I'm using 5.2.13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.