From 97eda21ab65cac19a9ca02612c916e526d114f99 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 20 Feb 2013 00:00:25 +0400 Subject: [PATCH 1/2] ErrorException now extends from Exception --- framework/base/ErrorException.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/framework/base/ErrorException.php b/framework/base/ErrorException.php index 073c1c3..90b5c20 100644 --- a/framework/base/ErrorException.php +++ b/framework/base/ErrorException.php @@ -15,8 +15,38 @@ namespace yii\base; * @author Alexander Makarov * @since 2.0 */ -class ErrorException extends \ErrorException +class ErrorException extends Exception { + protected $severity; + + /** + * Constructs the exception + * @link http://php.net/manual/en/errorexception.construct.php + * @param $message [optional] + * @param $code [optional] + * @param $severity [optional] + * @param $filename [optional] + * @param $lineno [optional] + * @param $previous [optional] + */ + public function __construct($message = '', $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, \Exception $previous = null) + { + parent::__construct($message, $code, $previous); + $this->severity = $severity; + $this->file = $filename; + $this->line = $lineno; + } + + /** + * Gets the exception severity + * @link http://php.net/manual/en/errorexception.getseverity.php + * @return int the severity level of the exception. + */ + final public function getSeverity() + { + return $this->severity; + } + public static function getFatalCodes() { return array(E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING); From 7f1f1b77674ea16eaa25a9d3a78dd715148bb2c8 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 20 Feb 2013 00:14:30 +0400 Subject: [PATCH 2/2] - Moved handling fatal errors into handleFatalError() method. - Refactored checking for fatal error. - Suppressed errors in exception handler in case of fatal errors. --- framework/base/Application.php | 22 +++++++++++++++------- framework/base/ErrorException.php | 10 ++++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/framework/base/Application.php b/framework/base/Application.php index 7c06e79..30810d6 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -150,10 +150,22 @@ class Application extends Module $this->afterRequest(); } + $this->handleFatalError(); + + if ($exit) { + exit($status); + } + } + + /** + * Handles fatal PHP errors + */ + public function handleFatalError() + { if(YII_ENABLE_ERROR_HANDLER) { $error = error_get_last(); - if(isset($error['type']) && in_array($error['type'], ErrorException::getFatalCodes())) { + if(ErrorException::isFatalErorr($error)) { unset($this->_memoryReserve); $exception = new ErrorException($error['message'], $error['type'], $error['type'], $error['file'], $error['line']); @@ -184,18 +196,14 @@ class Application extends Module $this->logException($exception); if (($handler = $this->getErrorHandler()) !== null) { - $handler->handle($exception); + @$handler->handle($exception); } else { $this->renderException($exception); } - $status = 1; + exit(1); } } - - if ($exit) { - exit($status); - } } /** diff --git a/framework/base/ErrorException.php b/framework/base/ErrorException.php index 90b5c20..a1e0252 100644 --- a/framework/base/ErrorException.php +++ b/framework/base/ErrorException.php @@ -47,9 +47,15 @@ class ErrorException extends Exception return $this->severity; } - public static function getFatalCodes() + /** + * Returns if error is one of fatal type + * + * @param array $error error got from error_get_last() + * @return bool if error is one of fatal type + */ + public static function isFatalErorr($error) { - return array(E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING); + return isset($error['type']) && in_array($error['type'], array(E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING)); } /**