PHP: Custom Error Handler - Handling Parse & Fatal Errors

PHP : Custom error handler - handling parse & fatal errors

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.

How do I handle parse and fatal errors?

If it can't parse your script, it won't be able to parse your custom error handler.

You should have display_errors off in your php.ini and also set error_reporting to none when your site is in production.

Also, I believe set_error_handler() can handle fatal errors.

How to Handle Parse error using register_shutdown_function() in php?

Parse errors can only be caught if they occur in scripts included or required. (also see https://stackoverflow.com/a/1900272/2123530)

So, sorry, this won't work the way you did it but can work this way :

<?php
register_shutdown_function('ShutDown');

include 'include.php';

function catchError($errno, $errstr, $errfile = '', $errline = ''){

echo "Eroor Type : " .$errno. "<br>";
echo "Eroor Message : " . $errstr . "<br>";
echo "Line Number : " . $errline;
exit();
}
function ShutDown(){
$lasterror = error_get_last();
if(in_array($lasterror['type'],Array( E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR, E_CORE_WARNING, E_COMPILE_WARNING, E_PARSE))){
catchError($lasterror['type'],$lasterror['message'],$lasterror['file'],$lasterror['line']);
}
}
?>

Content of include.php

<?php 
echo "Hi" // generate error == Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in file_naem on line 5;
echo "Hello";
?>

Can I really do nothing with fatal parse errors?

I don't think PHP can do anything with parse errors (or other errors during the compilation phase), but you should be able to configure your web server to display an error page of your choosing.

You don't say what web server you are using, but for example with Apache these are the Custom Error Response settings. Your errors will be HTTP 500 errors.

PHP handling fatal errors, and storing them in a file

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



Related Topics



Leave a reply



Submit