* @link http://netmonsters.ru * @package Majestic * @subpackage exception * @since 2010-02-26 */ class ErrorHandler { static public function init() { set_error_handler(array('Majestic\Exception\ErrorHandler', 'error_handler')); } static public function error_handler($errno, $errstr, $errfile, $errline) { $ob_handlers = ob_get_status(); if (!empty($ob_handlers)) { ob_end_clean(); } if (error_reporting() !== 0) { throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); } return true; } static public function logError($exception) { $error = 0; $exception_name = ''; if ($exception instanceof \ErrorException) { $error = $exception->getSeverity(); } else { $exception_name = get_class($exception) . ': '; } switch ($error) { case E_NOTICE: $error = 'Notice'; break; case E_WARNING: $error = 'Warning'; break; case E_ERROR: $error = 'Fatal Error'; break; default: $error = 'Unknown Error'; break; } $message = 'PHP ' . $error . ': ' . $exception_name . $exception->getMessage() . ' in ' . $exception->getFile() . ':' . $exception->getLine() . ' \nStack trace:\n' . $exception->getTraceAsString() . self::getHTTPErrorConditions(); // PHP Fatal error: Uncaught exception 'LogicException' in /www/test.tfs/face/htdocs/index.php:11\nStack trace:\n#0 {main}\n thrown in /www/test.tfs/face/htdocs/index.php on line 11, referer: http://test.tfs.manekeno.netmonsters.ru/news/create error_log($message); } static public function getHTTPErrorConditions() { $text = null; if (!is_null(\Majestic\Env::Server('REQUEST_METHOD')) && !is_null(\Majestic\Env::Server('REQUEST_URI'))) { $text = ', URL: ' . \Majestic\Env::Server('REQUEST_METHOD') . ' ' . \Majestic\Env::Server('REQUEST_URI'); $text .= ', referrer: ' . \Majestic\Env::Server('HTTP_REFERER'); } return $text; } static protected function getSource($file, $hiline) { $code = array(); $i = 0; foreach (file($file) as $line) { $i++; if ($i >= $hiline - 10 && $i <= $hiline + 10) { if ($i == $hiline) { $code[] = '
No ' . $name . ' data
'; } $text = 'Variable | Value |
---|---|
' . $key . ' | ' . $value . ' |
' . nl2br($trace) . '
';
}
/**
* @param \Exception $exception
* @return string
*/
static public function showDebug($exception)
{
$ob_handlers = ob_get_status();
if (!empty($ob_handlers)) {
ob_end_clean();
}
$class = get_class($exception);
$method = \Majestic\Env::Server('REQUEST_METHOD', '');
$uri = \Majestic\Env::getRequestUri();
$source = self::getSource($exception->getFile(), $exception->getLine());
$time = date('r', \Majestic\Env::Server('REQUEST_TIME', time()));
$trace = nl2br($exception->getTraceAsString());
$get = self::wrapArray(\Majestic\Env::Get(), 'GET');
$post = self::wrapArray(\Majestic\Env::Post(), 'POST');
$session = self::wrapArray(\Majestic\Session\Session::get(), 'SESSION');
$files = self::wrapArray(\Majestic\Env::Files(), 'FILES');
$cookies = self::wrapArray(\Majestic\Env::Cookie(), 'COOKIE');
$server = self::wrapArray(\Majestic\Env::Server(), 'SERVER');
$message = <<{$exception->getMessage()}
Request Method: | {$method} |
---|---|
Exception Type: | {$class} |
Exception Message: | {$exception->getMessage()} |
Exception Location: | {$exception->getFile()}, line {$exception->getLine()} |
Server time: | {$time} |
In file {$exception->getFile()}
, error at line {$exception->getLine()}
{$trace}
todo
You're seeing this error because you have defined constant DEBUG = True
in your config file. Change that to False
, and it will display a standard 500 page.