Skip to content

Instantly share code, notes, and snippets.

@Maykonn
Last active November 4, 2019 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maykonn/f052938ea5c0f86676a4fc9b81712ad2 to your computer and use it in GitHub Desktop.
Save Maykonn/f052938ea5c0f86676a4fc9b81712ad2 to your computer and use it in GitHub Desktop.
PHP Early fatal errors handler
<?php
// Early fatal errors handler, it will be replaced by a full featured one in Controller class
// (given it does not have any parse or fatal errors of its own)
function earlyFatalErrorHandler($unregister = false)
{
// Functionality for "unregistering" shutdown function
static $unregistered;
if ($unregister) $unregistered = true;
if ($unregistered) return;
// 1. error_get_last() returns NULL if error handled via set_error_handler
// 2. error_get_last() returns error even if error_reporting level less then error
$error = error_get_last();
// Fatal errors
$errorsToHandle = E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING;
if ((!defined('APP_PRODUCTION_MODE') || APP_PRODUCTION_MODE) && !is_null($error) && ($error['type'] & $errorsToHandle))
{
$message = 'FATAL ERROR: ' . $error['message'];
if (!empty($error['file'])) $message .= ' (' . $error['file'] . ' :' . $error['line']. ')';
error_log($message);
error_log(print_r($error, 1));
// Tell customer that you are aware of the issue and will take care of things
// echo "Apocalypse now!!!";
}
}
register_shutdown_function('earlyFatalErrorHandler');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment