From 43f256fd5e29bc1a4e8faa512a31cde1ebe66e29 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Mon, 17 Feb 2025 17:58:42 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=AE=80=E4=BC=98=E5=8C=96=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bootstrap/ConsoleErrorHandler.php | 43 +------------------- bootstrap/ErrorHandler.php | 66 +++++++++++++++++++++++++++++++ bootstrap/HttpErrorHandler.php | 43 +------------------- 3 files changed, 70 insertions(+), 82 deletions(-) create mode 100644 bootstrap/ErrorHandler.php diff --git a/bootstrap/ConsoleErrorHandler.php b/bootstrap/ConsoleErrorHandler.php index daf794f5..6eb0f9ab 100644 --- a/bootstrap/ConsoleErrorHandler.php +++ b/bootstrap/ConsoleErrorHandler.php @@ -8,21 +8,15 @@ namespace Bootstrap; use App\Library\Logger as AppLogger; -use Phalcon\Config as PhConfig; -use Phalcon\Di\Injectable; use Phalcon\Logger\Adapter\File as PhLogger; use Throwable; -class ConsoleErrorHandler extends Injectable +class ConsoleErrorHandler extends ErrorHandler { public function __construct() { - set_exception_handler([$this, 'handleException']); - - set_error_handler([$this, 'handleError']); - - register_shutdown_function([$this, 'handleShutdown']); + parent::__construct(); } /** @@ -50,39 +44,6 @@ class ConsoleErrorHandler extends Injectable echo $content . PHP_EOL; } - public function handleError($errNo, $errStr, $errFile, $errLine) - { - if (in_array($errNo, [E_WARNING, E_NOTICE, E_DEPRECATED, E_USER_WARNING, E_USER_NOTICE, E_USER_DEPRECATED])) { - return true; - } - - $logger = $this->getLogger(); - - $logger->error("Error [{$errNo}]: {$errStr} in {$errFile} on line {$errLine}"); - - return false; - } - - public function handleShutdown() - { - $error = error_get_last(); - - if ($error !== NULL && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) { - - $logger = $this->getLogger(); - - $logger->error("Fatal Error [{$error['type']}]: {$error['message']} in {$error['file']} on line {$error['line']}"); - } - } - - /** - * @return PhConfig - */ - protected function getConfig() - { - return $this->getDI()->getShared('config'); - } - /** * @return PhLogger */ diff --git a/bootstrap/ErrorHandler.php b/bootstrap/ErrorHandler.php new file mode 100644 index 00000000..4a279d1b --- /dev/null +++ b/bootstrap/ErrorHandler.php @@ -0,0 +1,66 @@ +getLogger(); + + $logger->error("Error [{$errNo}]: {$errMsg} in {$errFile} on line {$errLine}"); + + return false; + } + + public function handleShutdown() + { + $error = error_get_last(); + + if ($error !== NULL && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) { + + $logger = $this->getLogger(); + + $logger->error("Fatal Error [{$error['type']}]: {$error['message']} in {$error['file']} on line {$error['line']}"); + } + } + + /** + * @return Config + */ + protected function getConfig() + { + return $this->getDI()->getShared('config'); + } + + /** + * @param Throwable $e + */ + abstract public function handleException($e); + + abstract protected function getLogger(); + +} diff --git a/bootstrap/HttpErrorHandler.php b/bootstrap/HttpErrorHandler.php index e7f3b1ad..ab403b52 100644 --- a/bootstrap/HttpErrorHandler.php +++ b/bootstrap/HttpErrorHandler.php @@ -13,20 +13,14 @@ use App\Exceptions\NotFound as NotFoundException; use App\Exceptions\ServiceUnavailable as ServiceUnavailableException; use App\Exceptions\Unauthorized as UnauthorizedException; use App\Library\Logger as AppLogger; -use Phalcon\Config; -use Phalcon\Di\Injectable; use Throwable; -class HttpErrorHandler extends Injectable +class HttpErrorHandler extends ErrorHandler { public function __construct() { - set_exception_handler([$this, 'handleException']); - - set_error_handler([$this, 'handleError']); - - register_shutdown_function([$this, 'handleShutdown']); + parent::__construct(); } /** @@ -49,31 +43,6 @@ class HttpErrorHandler extends Injectable } } - public function handleError($errNo, $errStr, $errFile, $errLine) - { - if (in_array($errNo, [E_WARNING, E_NOTICE, E_DEPRECATED, E_USER_WARNING, E_USER_NOTICE, E_USER_DEPRECATED])) { - return true; - } - - $logger = $this->getLogger(); - - $logger->error("Error [{$errNo}]: {$errStr} in {$errFile} on line {$errLine}"); - - return false; - } - - public function handleShutdown() - { - $error = error_get_last(); - - if ($error !== NULL && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) { - - $logger = $this->getLogger(); - - $logger->error("Fatal Error [{$error['type']}]: {$error['message']} in {$error['file']} on line {$error['line']}"); - } - } - /** * @param Throwable $e */ @@ -165,14 +134,6 @@ class HttpErrorHandler extends Injectable ]; } - /** - * @return Config - */ - protected function getConfig() - { - return $this->getDI()->getShared('config'); - } - protected function getLogger() { $logger = new AppLogger();