bastman / processus-kindergarten Goto Github PK
View Code? Open in Web Editor NEWhelper for bootstrapping, errorhandling and sandboxing crappy code legacy libs
helper for bootstrapping, errorhandling and sandboxing crappy code legacy libs
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);
}
public static function handleShutdown()
{
$error = error_get_last();
if ($error === null) {
// no error, we have a "normal" shut down (script is finished).
return;
}
//var_dump(METHOD);
//var_dump($error);
// an error occurred and forced the shut down
$e = new Lib_Application_Exception(self::ERROR_SHUTDOWN);
$e->setMethod(METHOD);
$e->setFault(array(
"lastError" => $error,
));
self::handleUncaughtException($e);
}
from an older project ..
// dirty hack for white pages issues
if (
( ((int)$errorNumber) ===8)
&& (fnmatch("Undefined variable*",$errorString, false))
) {
// the bug appears if you d sth like...
/*
$x=$bla->blug(); // where bla is undefined
*/
try {
$e = new Lib_Application_Exception($errorString);
$e->setFault(array(
$errorNumber,
$errorString,
$errorFile,
$errorLine
));
throw $e;
}catch(Exception $e) {
//var_dump($e->getMessage());
self::handleUncaughtException($e);
die("DIED at ".__METHOD__.__LINE__); // just in case
}
}
.
If raise an error, that has not been catched (by error_reporting_level), it will be available in shutdown function. And finally onShutDownFailed will be called. (But it should not!!!! since I wanted to ignore that error using capture level)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.