1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-27 04:57:39 +08:00

优化错误处理机制

This commit is contained in:
xiaochong0302 2025-02-17 12:30:03 +08:00
parent ca67c15548
commit aa641e5169
2 changed files with 60 additions and 1 deletions

View File

@ -11,6 +11,7 @@ use App\Library\Logger as AppLogger;
use Phalcon\Config as PhConfig; use Phalcon\Config as PhConfig;
use Phalcon\Di\Injectable; use Phalcon\Di\Injectable;
use Phalcon\Logger\Adapter\File as PhLogger; use Phalcon\Logger\Adapter\File as PhLogger;
use Throwable;
class ConsoleErrorHandler extends Injectable class ConsoleErrorHandler extends Injectable
{ {
@ -18,10 +19,14 @@ class ConsoleErrorHandler extends Injectable
public function __construct() public function __construct()
{ {
set_exception_handler([$this, 'handleException']); set_exception_handler([$this, 'handleException']);
set_error_handler([$this, 'handleError']);
register_shutdown_function([$this, 'handleShutdown']);
} }
/** /**
* @param \Throwable $e * @param Throwable $e
*/ */
public function handleException($e) public function handleException($e)
{ {
@ -45,6 +50,31 @@ class ConsoleErrorHandler extends Injectable
echo $content . PHP_EOL; 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 * @return PhConfig
*/ */

View File

@ -23,6 +23,10 @@ class HttpErrorHandler extends Injectable
public function __construct() public function __construct()
{ {
set_exception_handler([$this, 'handleException']); set_exception_handler([$this, 'handleException']);
set_error_handler([$this, 'handleError']);
register_shutdown_function([$this, 'handleShutdown']);
} }
/** /**
@ -45,6 +49,31 @@ 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 * @param Throwable $e
*/ */