17

I have a simple custom error handler that writes in a error log file some useful debug infos.

it's work for everything but it's not get triggered for FATAL error.

Any way to solve this?

Currently to bypass this circumstance I have registered a shutdown function too that checks error_get_last()

Cœur
  • 37,241
  • 25
  • 195
  • 267
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • 5
    [`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.`](http://us.php.net/set_error_handler) – animuson Dec 15 '11 at 23:13
  • 1
    This question misses a link to: [PHP : Custom error handler - handling parse & fatal errors](http://stackoverflow.com/q/1900208/367456). – hakre Jun 22 '12 at 10:43

3 Answers3

26

Nope, that's just a limitation of set_error_handler(); it doesn't handle all errors.

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.

The register_shutdown_function() and error_get_last() is a decent workaround.

Daniel James
  • 3,899
  • 22
  • 30
alex
  • 479,566
  • 201
  • 878
  • 984
  • Can you give example of using register_shutdown_function() and error_get_last() as workaround i can't think of one. – Roman Toasov Nov 21 '16 at 20:57
5

There are only hackish ways to solve it, e.g. by using register_shutdown_function() and then checking if an error occurred inside that function.

PHP has log_errors for a reason, you can make PHP log any error to syslog or a logfile without a single line of custom code. So using set_error_handler() for this purpose is not needed at all and should be avoided unless you need e.g. a stacktrace.

alex
  • 479,566
  • 201
  • 878
  • 984
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
4

As others have pointed out we can use register_shutdown_function() and error_get_last() in following fashion.

The below implementation will catch the errors which are not even caught by \Throwable as tested in php 7.1. It should work for previous PHP versions too. It should only be implemented in your development environment(by just adding it in your development config file) and shouldn't be done in production environment.

Implementation

register_shutdown_function(function () {
    $err = error_get_last();
    if (! is_null($err)) {
        print 'Error#'.$err['message'].'<br>';
        print 'Line#'.$err['line'].'<br>';
        print 'File#'.$err['file'].'<br>';
    }
});

Example Error

Error# Class Path/To/MyService contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Path/To/MyServiceInterface::add)
Line# 12
File# Path/To/MyService.php
Kamal Soni
  • 1,522
  • 13
  • 15
  • is there a way to resume code a fatal error happened? for an error says some function is not defined. i want to define that function using eval() and resume my code to the next error. is that possible? – Ahmad Ameri Jul 29 '20 at 19:05
  • @AhmadAmeri Unfortunately, there is no way to continue after fatal error has occurred in PHP. You only can catch them through this hack and systematically remove them as and when they occur. – Kamal Soni Jul 31 '20 at 02:32