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();