mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-24 20:06:09 +08:00
精简优化错误处理机制
This commit is contained in:
parent
aa641e5169
commit
43f256fd5e
@ -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
|
||||
*/
|
||||
|
66
bootstrap/ErrorHandler.php
Normal file
66
bootstrap/ErrorHandler.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
|
||||
* @license https://opensource.org/licenses/GPL-2.0
|
||||
* @link https://www.koogua.com
|
||||
*/
|
||||
|
||||
namespace Bootstrap;
|
||||
|
||||
use Phalcon\Config;
|
||||
use Phalcon\Di\Injectable;
|
||||
use Throwable;
|
||||
|
||||
abstract class ErrorHandler extends Injectable
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
set_exception_handler([$this, 'handleException']);
|
||||
|
||||
set_error_handler([$this, 'handleError']);
|
||||
|
||||
register_shutdown_function([$this, 'handleShutdown']);
|
||||
}
|
||||
|
||||
public function handleError($errNo, $errMsg, $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}]: {$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();
|
||||
|
||||
}
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user