123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- namespace yii\base;
- use Yii;
- use yii\helpers\VarDumper;
- use yii\web\HttpException;
- abstract class ErrorHandler extends Component
- {
-
- public $discardExistingOutput = true;
-
- public $memoryReserveSize = 262144;
-
- public $exception;
-
- private $_memoryReserve;
-
- private $_hhvmException;
-
- public function register()
- {
- ini_set('display_errors', false);
- set_exception_handler([$this, 'handleException']);
- if (defined('HHVM_VERSION')) {
- set_error_handler([$this, 'handleHhvmError']);
- } else {
- set_error_handler([$this, 'handleError']);
- }
- if ($this->memoryReserveSize > 0) {
- $this->_memoryReserve = str_repeat('x', $this->memoryReserveSize);
- }
- register_shutdown_function([$this, 'handleFatalError']);
- }
-
- public function unregister()
- {
- restore_error_handler();
- restore_exception_handler();
- }
-
- public function handleException($exception)
- {
- if ($exception instanceof ExitException) {
- return;
- }
- $this->exception = $exception;
-
- $this->unregister();
-
-
- if (PHP_SAPI !== 'cli') {
- http_response_code(500);
- }
- try {
- $this->logException($exception);
- if ($this->discardExistingOutput) {
- $this->clearOutput();
- }
- $this->renderException($exception);
- if (!YII_ENV_TEST) {
- \Yii::getLogger()->flush(true);
- if (defined('HHVM_VERSION')) {
- flush();
- }
- exit(1);
- }
- } catch (\Exception $e) {
-
- $this->handleFallbackExceptionMessage($e, $exception);
- } catch (\Throwable $e) {
-
- $this->handleFallbackExceptionMessage($e, $exception);
- }
- $this->exception = null;
- }
-
- protected function handleFallbackExceptionMessage($exception, $previousException)
- {
- $msg = "An Error occurred while handling another error:\n";
- $msg .= (string) $exception;
- $msg .= "\nPrevious exception:\n";
- $msg .= (string) $previousException;
- if (YII_DEBUG) {
- if (PHP_SAPI === 'cli') {
- echo $msg . "\n";
- } else {
- echo '<pre>' . htmlspecialchars($msg, ENT_QUOTES, Yii::$app->charset) . '</pre>';
- }
- } else {
- echo 'An internal server error occurred.';
- }
- $msg .= "\n\$_SERVER = " . VarDumper::export($_SERVER);
- error_log($msg);
- if (defined('HHVM_VERSION')) {
- flush();
- }
- exit(1);
- }
-
- public function handleHhvmError($code, $message, $file, $line, $context, $backtrace)
- {
- if ($this->handleError($code, $message, $file, $line)) {
- return true;
- }
- if (E_ERROR & $code) {
- $exception = new ErrorException($message, $code, $code, $file, $line);
- $ref = new \ReflectionProperty('\Exception', 'trace');
- $ref->setAccessible(true);
- $ref->setValue($exception, $backtrace);
- $this->_hhvmException = $exception;
- }
- return false;
- }
-
- public function handleError($code, $message, $file, $line)
- {
- if (error_reporting() & $code) {
-
-
- if (!class_exists('yii\\base\\ErrorException', false)) {
- require_once __DIR__ . '/ErrorException.php';
- }
- $exception = new ErrorException($message, $code, $code, $file, $line);
-
- $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
- array_shift($trace);
- foreach ($trace as $frame) {
- if ($frame['function'] === '__toString') {
- $this->handleException($exception);
- if (defined('HHVM_VERSION')) {
- flush();
- }
- exit(1);
- }
- }
- throw $exception;
- }
- return false;
- }
-
- public function handleFatalError()
- {
- unset($this->_memoryReserve);
-
-
- if (!class_exists('yii\\base\\ErrorException', false)) {
- require_once __DIR__ . '/ErrorException.php';
- }
- $error = error_get_last();
- if (ErrorException::isFatalError($error)) {
- if (!empty($this->_hhvmException)) {
- $exception = $this->_hhvmException;
- } else {
- $exception = new ErrorException($error['message'], $error['type'], $error['type'], $error['file'], $error['line']);
- }
- $this->exception = $exception;
- $this->logException($exception);
- if ($this->discardExistingOutput) {
- $this->clearOutput();
- }
- $this->renderException($exception);
-
- Yii::getLogger()->flush(true);
- if (defined('HHVM_VERSION')) {
- flush();
- }
- exit(1);
- }
- }
-
- abstract protected function renderException($exception);
-
- public function logException($exception)
- {
- $category = get_class($exception);
- if ($exception instanceof HttpException) {
- $category = 'yii\\web\\HttpException:' . $exception->statusCode;
- } elseif ($exception instanceof \ErrorException) {
- $category .= ':' . $exception->getSeverity();
- }
- Yii::error($exception, $category);
- }
-
- public function clearOutput()
- {
-
- for ($level = ob_get_level(); $level > 0; --$level) {
- if (!@ob_end_clean()) {
- ob_clean();
- }
- }
- }
-
- public static function convertExceptionToError($exception)
- {
- trigger_error(static::convertExceptionToString($exception), E_USER_ERROR);
- }
-
- public static function convertExceptionToString($exception)
- {
- if ($exception instanceof UserException) {
- return "{$exception->getName()}: {$exception->getMessage()}";
- }
- if (YII_DEBUG) {
- return static::convertExceptionToVerboseString($exception);
- }
- return 'An internal server error occurred.';
- }
-
- public static function convertExceptionToVerboseString($exception)
- {
- if ($exception instanceof Exception) {
- $message = "Exception ({$exception->getName()})";
- } elseif ($exception instanceof ErrorException) {
- $message = (string)$exception->getName();
- } else {
- $message = 'Exception';
- }
- $message .= " '" . get_class($exception) . "' with message '{$exception->getMessage()}' \n\nin "
- . $exception->getFile() . ':' . $exception->getLine() . "\n\n"
- . "Stack trace:\n" . $exception->getTraceAsString();
- return $message;
- }
- }
|