from an old project ...
/**
* @static
* @param Exception|null $exception
* @return void
*/
public static function handleUncaughtException($exception)
{
$errorUserMessage = "An Error occured. Please retry lateron!";
try {
$outputBuffer = ob_get_contents();
ob_end_clean();
try {
//if (headers_sent()!==true) {
//header("HTTP/1.0 500"); // 500 makes trouble in vz env!
//}
} catch (Exception $e) {
}
if (($exception instanceof Exception) !== true) {
die(__METHOD__ . " FAILED! Invalid Parameter 'exception'.");
}
$isDebugMode = false;
$isDeveloper = false;
try {
$isDeveloper = self::getRegistry()->getDebug()->isDeveloper();
} catch (Exception $e) {
var_dump($e);
die(__METHOD__ . " FAILED! Invalid 'isDebugMode'.");
}
try {
$isDebugMode = self::getRegistry()->getDebug()->isDebugMode();
} catch (Exception $e) {
die(__METHOD__ . " FAILED! Invalid 'isDeveloper'.");
}
$showDebugInfo = (
($isDebugMode === true)
// || ($isDeveloper === true)
);
if ($showDebugInfo !== true) {
try {
if($exception instanceof Lib_Application_Exception) {
/**
* @var $appException Lib_Application_Exception
*/
$appException = $exception;
if(trim(''.$appException->getUserMessage()) !== "" ) {
$errorUserMessage = '' .
$appException->getUserMessage();
}
}
}catch(Exception $ex) {
//NOP
}
die(" " . $errorUserMessage . " (E0021)");
}
$errorInfo = array(
"class" => get_class($exception),
"message" => $exception->getMessage(),
"method" => null,
"methodLine" => null,
"file" => $exception->getFile(),
"line" => $exception->getLine(),
"stackTrace" => $exception->getTrace(),
"stackTraceAsString" => $exception->getTraceAsString(),
"fault" => null,
"lastError" => array(
"type" => null,
"file" => null,
"line" => null,
"message" => null,
),
);
if (defined("ROOT_PATH")) {
$errorInfo["file"] = str_replace(ROOT_PATH, '', $errorInfo['file']);
$errorInfo["stackTraceAsString"] = str_replace(
ROOT_PATH, '', $errorInfo['stackTraceAsString']
);
$outputBuffer = str_replace(ROOT_PATH, '', $outputBuffer);
}
if ($exception instanceof Lib_Application_Exception) {
/**
* @var Lib_Application_Exception $exception
*/
$errorInfo["method"] = $exception->getMethod();
$errorInfo["fault"] = $exception->getFault();
$errorInfo["methodLine"] = $exception->getMethodLine();
$errorInfo["fault"] = $exception->getFault();
}
$isShutdownError = false;
if (
($exception instanceof Lib_Application_Exception)
&& ($exception->getMessage() === self::ERROR_SHUTDOWN)
) {
$isShutdownError = true;
}
// parse last error
$lastError = error_get_last();
if (is_array($lastError)) {
foreach ($lastError as $key => $value) {
$errorInfo["lastError"][$key] = $value;
}
}
if (defined("ROOT_PATH")) {
$errorInfo["lastError"]["file"] = str_replace(
ROOT_PATH, '', $errorInfo["lastError"]['file']
);
}
// +++++++++++++++++ simple wildfire output ++++++++++++++++++
try {
//if (headers_sent()!==true) {
header('X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
header('X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3');
header('X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
$description = $errorInfo['message'] . ' in ' . $errorInfo['file'] . ' line ' . $errorInfo['line'];
//$msg = '[{"Type":"LOG","File":"'.$error['file'].'","Line":'.$error['line'].'},"'.$description.'"]';
//NOTICE msg must not have newlines
$msg = array(
"Description" => $description,
"Type" => "LOG",
"File" => $errorInfo['file'],
"Line" => $errorInfo['line'],
);
$msg = json_encode($msg);
header('X-Wf-1-1-1-1: ' . strlen($msg) . '|' . $msg . '|');
//}
} catch (Exception $e) {
// e.g.: "HEADERS ALREADY SENT"
}
// ++++++++++++++++++ html nice output ++++++++++++++++++++
$outHtmlText = '';
if ($isShutdownError === true) {
$outHtmlText .= '<b>----------- FATAL ERROR (SHUTDOWN)! ------------ </b>';
} else {
$outHtmlText .= '<b>----------- FATAL ERROR (UNCAUGHT EXCEPTION CATCHED)! ------------ </b>';
}
$outHtmlText .= ""
. '<br /><br />'
. '[' . __METHOD__ . ' '
. ' isDebugMode=' . json_encode($isDebugMode)
. ' isDeveloper=' . json_encode($isDeveloper)
. " ]"
. '<br /><br />'
. '<b>' . $errorInfo["class"] . "</b>"
. ' ' . htmlentities($errorInfo["message"])
. '<br /><br />'
. ' ' . $errorInfo["method"] . " " . $errorInfo["methodLine"]
. ' ' . $errorInfo["file"] . " " . $errorInfo["line"]
. '<br /><br />'
. htmlentities($errorInfo["stackTraceAsString"])
. '<br /><br />';
ob_start();
var_dump($errorInfo["lastError"]);
$lastErrorDump = ob_get_contents();
ob_clean();
if (defined("ROOT_PATH")) {
$lastErrorDump = str_replace(ROOT_PATH, '', $lastErrorDump);
}
$outHtmlText .= ''
. '<b>------------- LAST ERROR DUMP --------------------</b>'
. '<br /><br />'
. htmlentities($lastErrorDump)
. '<br /><br />';
ob_start();
var_dump($errorInfo["fault"]);
$faultDump = ob_get_contents();
ob_clean();
if (defined("ROOT_PATH")) {
$faultDump = str_replace(ROOT_PATH, '', $faultDump);
}
$outHtmlText .= ''
. "<b>----------- FAULT DUMP ------------------ </b>"
. '<br /><br />'
. htmlentities($faultDump)
. '<br /><br />';
if (!$errorInfo["stackTrace"]) {
$errorInfo["stackTrace"] = debug_backtrace();
}
$trace = $errorInfo["stackTrace"];
foreach ($trace as $key => $stackPoint) {
// I'm converting arguments to their type
// (prevents passwords from ever getting logged as anything other than 'string')
$args = array();
if (isset($trace[$key]['args'])) {
$args = $trace[$key]['args'];
}
$_args = array();
foreach ($args as $arg) {
$argValue = $arg;
$argType = gettype($arg);
$argText = "" . $argType;
if (is_object($arg)) {
$argClass = get_class($arg);
if ($argClass) {
$argText .= " " . $argClass;
}
}
$_args[] = $argText;
}
//$trace[$key]['args'] = array_map('gettype', $trace[$key]['args']);
$trace[$key]['args'] = $_args;
}
ob_start();
var_dump($trace);
$stackTraceDump = ob_get_contents();
ob_clean();
if (defined("ROOT_PATH")) {
$stackTraceDump = str_replace(ROOT_PATH, '', $stackTraceDump);
}
$outHtmlText .= ''
. '<br /><br />'
. "<b>--------------- STACKTRACE DUMP--------------</b>"
. '<br /><br />'
. htmlentities($stackTraceDump)
. '<br /><br />';
if (defined("ROOT_PATH")) {
$outputBuffer = str_replace(ROOT_PATH, '', $outputBuffer);
}
$outHtmlText .= ''
. "<b>------------- OUTPUTBUFFER DUMP ------------------</b>"
. '<br /><br />'
. htmlentities($outputBuffer)
. '<br /><br />';
echo
'<div style="font-size: 16px; padding: 16px 32px 16px 32px; margin: 16px; border: solid red 3px; background: #000000; color: red;"><pre>'
. $outHtmlText
. '</pre></div>';
} catch (Exception $e) {
//var_dump($e);
die("An Error occured. Please retry lateron! (E0022)");
}
die("DIED AT ".__METHOD__);
}
/**
* @static
* @param Exception|null $exception
* @return void
*/
public static function handleUncaughtExceptionCli($exception)
{
var_dump($exception);
exit(1);
}
$error = error_get_last();
if ($error === null) {
// no error, we have a "normal" shut down (script is finished).
return;
}
self::handleUncaughtException($e);
}