Php Fatal Error Handler
Contents |
a custom error handlerImplementing a custom error handler using set_error_handler() in PHP can be php register_shutdown_function a useful technique (Google search for more info/examples)Unfortunately,
Php Set_error_handler
set_error_handler() doesn’t catch fatal errors — as the PHP docs say:The following error types
Php Catch Fatal Error And Continue
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
Php Set_exception_handler
in the file where set_error_handler() is called.Handily, I just discovered it’s possible to catch fatal E_ERROR errors and direct them to your custom error handler using a combination of register_shutdown_function() and error_get_last():set_error_handler('myErrorHandler');register_shutdown_function('fatalErrorShutdownHandler');function myErrorHandler($code, $message, $file, $line) { ...}function fatalErrorShutdownHandler(){ $last_error what is fatal error in php = error_get_last(); if ($last_error['type'] === E_ERROR) { // fatal error myErrorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); }}The key is that functions registered with register_shutdown_function() are called even on a fatal error - including out of memory errors. error_get_last() can then be used to detect whether we’re ending the script because of a fatal error, and pass the error info to your custom error handler if so.UncategorizedDevPHPBlockedUnblockFollowFollowingAndy YoungStartups. Code. Product, Growth & Metrics. EIR @500startups. Previously UK @Stripe, founder @GroupSpaces.FollowAndy’s BlogEntrepreneur, engineer, music addict. Consulting on growth, product & analytics, and Distro team @ 500 Startups. Previously launched Stripe in the UK, founder of GroupSpaces, creator of Selective Tweets (#fb). Love/live in London.×Don’t miss Andy Young’s next storyBlockedUnblockFollowFollowingAndy Young
a custom error handlerImplementing a custom error handler using set_error_handler() in PHP can be php error types a useful technique (Google search for more info/examples)Unfortunately, php recover from fatal error set_error_handler() doesn’t catch fatal errors — as the PHP docs say:The following error types php error_get_last 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 https://insomanic.me.uk/php-trick-catching-fatal-errors-e-error-with-a-custom-error-handler-cea2262697a2 in the file where set_error_handler() is called.Handily, I just discovered it’s possible to catch fatal E_ERROR errors and direct them to your custom error handler using a combination of register_shutdown_function() and error_get_last():set_error_handler('myErrorHandler');register_shutdown_function('fatalErrorShutdownHandler');function myErrorHandler($code, $message, $file, $line) { ...}function fatalErrorShutdownHandler(){ $last_error https://insomanic.me.uk/php-trick-catching-fatal-errors-e-error-with-a-custom-error-handler-cea2262697a2 = error_get_last(); if ($last_error['type'] === E_ERROR) { // fatal error myErrorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); }}The key is that functions registered with register_shutdown_function() are called even on a fatal error - including out of memory errors. error_get_last() can then be used to detect whether we’re ending the script because of a fatal error, and pass the error info to your custom error handler if so.UncategorizedDevPHPBlockedUnblockFollowFollowingAndy YoungStartups. Code. Product, Growth & Metrics. EIR @500startups. Previously UK @Stripe, founder @GroupSpaces.FollowAndy’s BlogEntrepreneur, engineer, music addict. Consulting on growth, product & analytics, and Distro team @ 500 Startups. Previously launched Stripe in the UK, founder of GroupSpaces, creator of Selective Tweets (#fb). Love/live in London.×Don’t miss Andy Young’s next storyBlockedUnblockFollowFollowingAndy Young
of errors. For example, if you try to call a non-existing function from your http://markonphp.com/handling-fatal-errors-php script, PHP will probably render a white page on your production server… if your settings are like the one I defined in my article about error display. Using register_shutdown_function() For Fatal Errors The solution to this isn’t the cleanest I found, but it works. We’ll use the method register_shutdown_function(). Keep in mind fatal error that this method will be called after the end of each one of your scripts! So, don’t do any super costly operations in this custom handler, or all of your scripts will be slow. Add this piece of code to the previous script we made in the post : register_shutdown_function('fatalErrorHandler'); function fatalErrorHandler() { $last_error = error_get_last(); if ($last_error['type'] === E_ERROR) { errorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); } } The 1st line is the line registering our custom 'shutdown function'. You can register as many function as you want with this function. For this example, we only need to register one handler that will check for fatal errors. The 4th line inside our custom shutdown handler is a piece of code that will get the last error that happened (if there was an error). The 5th line will check that the error is a fatal error. We don't want this handler to handle other types of errors because we already do this with our custom error