/** Custom error-to-exception handler
*
* courtesy of [email protected]
* http://php.net/manual/en/class.errorexception.php#95415
* As noted below [in the discussion], it's important to realize that unless
* caught, any Exception thrown will halt the script. So converting EVERY
* notice, warning, or error to an ErrorException will halt your script when
* something harmlesss like E_USER_NOTICE is triggered.
*
* [snip]
*
* Setting this function as the error handler will result in ErrorExceptions
* only being thrown for E_USER_ERROR and E_RECOVERABLE_ERROR, while other
* enabled error types will simply get error_log()'ed.
*
* It's worth noting again that no matter what you do, "E_ERROR, E_PARSE,
* E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most
* of E_STRICT" will never reach your custom error handler, and therefore will
* not be converted into ErrorExceptions. Plan accordingly.
*
* @param integer $severity PHP Error severity.
* @param string $message Error text.
* @param string $filename Error source file.
* @param integer $lineno Error source line number.
* @param mixed $context Error context.
* @return boolean <b>FALSE</b> if error is enabled in configuration, othervise undefined.
*/
static function errorHandler($severity, $message, $filename, $lineno, $context)
{
// If this error is of the enabled ones in php config (php.ini, .htaccess, etc)
if((bool)($severity & ini_get('error_reporting')))
{
// -- FATAL ERROR
// throw an Error Exception, to be handled by whatever Exception handling logic is available in this context
if(in_array($severity, array(E_USER_ERROR, E_RECOVERABLE_ERROR)))
{
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
else if(TOOL_RETHROW_ALL)
{
$e = new ErrorException($message, 0, $severity, $filename, $lineno);
print("<p>(Sev:{$e->getSeverity()}) " . htmlspecialchars($e->getMessage()) . "</p>\n");
print("<pre>Stack trace:\n" . htmlspecialchars($e->getTraceAsString()) . "\n" .
" thrown in <b>" . htmlspecialchars($e->getFile()) . ":{$e->getLine()}</b></pre>\n");
// Assuming debugging session in progress, we keep system log clean from
// excessive error spam, forbidding error tracking since error is displayed.
return true;
}
// -- NON-FATAL ERROR/WARNING/NOTICE
// Log the error since it's enabled
error_log($message, 0);
// Make sure this ends up in $php_errormsg, if appropriate
return false;
}
}