PHP : Custom error handler - handling parse & fatal errors PHP : Custom error handler - handling parse & fatal errors php php

PHP : Custom error handler - handling parse & fatal errors


Actually you can handle parse and fatal errors. It is true that the error handler function you defined with set_error_handler() will not be called. The way to do it is by defining a shutdown function with register_shutdown_function(). Here is what I have working in my website:

File prepend.php (this file will be prepended to all php scripts automatically). See below for tips on prepending files to PHP.

set_error_handler("errorHandler");register_shutdown_function("shutdownHandler");function errorHandler($error_level, $error_message, $error_file, $error_line, $error_context){$error = "lvl: " . $error_level . " | msg:" . $error_message . " | file:" . $error_file . " | ln:" . $error_line;switch ($error_level) {    case E_ERROR:    case E_CORE_ERROR:    case E_COMPILE_ERROR:    case E_PARSE:        mylog($error, "fatal");        break;    case E_USER_ERROR:    case E_RECOVERABLE_ERROR:        mylog($error, "error");        break;    case E_WARNING:    case E_CORE_WARNING:    case E_COMPILE_WARNING:    case E_USER_WARNING:        mylog($error, "warn");        break;    case E_NOTICE:    case E_USER_NOTICE:        mylog($error, "info");        break;    case E_STRICT:        mylog($error, "debug");        break;    default:        mylog($error, "warn");}}function shutdownHandler() //will be called when php script ends.{$lasterror = error_get_last();switch ($lasterror['type']){    case E_ERROR:    case E_CORE_ERROR:    case E_COMPILE_ERROR:    case E_USER_ERROR:    case E_RECOVERABLE_ERROR:    case E_CORE_WARNING:    case E_COMPILE_WARNING:    case E_PARSE:        $error = "[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line'];        mylog($error, "fatal");}}function mylog($error, $errlvl){...do whatever you want...}

PHP will call function errorHandler() if he catches an error in any of the scripts. If the error forces the script to shutdown immediately, the error is handled by function shutdownHandler().

This is working on the site I have under development. I haven't yet tested it in production. But it is currently catching all errors I find while developing it.

I believe there is a risk of catching the same error twice, once by each function. This could happen if an error that I am handling in the function shutdownHandler() was also caught by function errorHandler().

TODO's:

1 - I need to work on a better log() function to handle errors gracefully. Because I am still in development, I am basically logging the error to a database and echoing it to the screen.

2 - Implement error handling for all MySQL calls.

3 - Implement error handling for my javascript code.

IMPORTANT NOTES:

1 - I am using the following line in my php.ini to automatically prepend the above script to all php scripts:

auto_prepend_file = "/homepages/45/d301354504/htdocs/hmsee/cgi-bin/errorhandling.php"

it works well.

2 - I am logging and resolving all errors, including E_STRICT errors. I believe in developing a clean code. During development, my php.ini file has the following lines:

track_errors = 1display_errors = 1error_reporting = 2147483647html_errors = 0

When I go live, I will change display_errors to 0 to reduce the risk of my users seeing ugly PHP error messages.

I hope this helps someone.


Simple Answer: You can't. See the manual:

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, 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.

For every other error, you can use set_error_handler()

EDIT:

Since it seems, that there are some discussions on this topic, with regards to using register_shutdown_function, we should take a look at the definition of handling: To me, handling an error means catching the error and reacting in a way that is "nice" for the user and the underlying data (databases, files, web services, etc.).

Using register_shutdown_function you cannot handle an error from within the code where it was called, meaning the code would still stop working at the point where the error occurs. You can, however, present the user with an error message instead of a white page, but you cannot, for example, roll back anything that your code did prior to failing.


You can track these errors using code like this:

(Parse errors can only be caught if they occur in other script files via include() or require(), or by putting this code into an auto_prepend_file as other answers have mentioned.)

function shutdown() {    $isError = false;    if ($error = error_get_last()){    switch($error['type']){        case E_ERROR:        case E_CORE_ERROR:        case E_COMPILE_ERROR:        case E_USER_ERROR:            $isError = true;            break;        }    }    if ($isError){        var_dump ($error);//do whatever you need with it    }}register_shutdown_function('shutdown');